'I got problem while I change static product become to firebase database in Flutter
I got problem when try to display product data from firebase. I actually check the connection and got no problem.I got the product data but when try to display product data is not show. what I do mistake?
File FirestoreDB
class FirestoreDB {
final FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
Stream<List<Product>> getData() {
return _firebaseFirestore
.collection("Product")
.snapshots()
.map((snapshot) {
return snapshot.docs.map((docs) => Product.fromSnapshot(docs)).toList();
});
}
}
File Product
class Product {
final String image, title, des, catname;
final int price, star;
final Color bgColor;
Product({
required this.image,
required this.title,
required this.des,
required this.price,
required this.star,
required this.catname,
this.bgColor = const Color(0xFFEFEFF2),
});
static Product fromSnapshot(DocumentSnapshot snap) {
Product product = Product(
image: snap['image'],
title: snap['title'],
des: snap['des'],
price: snap['price'],
star: snap['star'],
catname: snap['catname']
);
return product;
}
}
I debug and check this and I found data from firebase
File Product Controller
class ProductController extends GetxController {
final product = <Product>[].obs;
@override
void onInit() {
product.bindStream(FirestoreDB().getData());
super.onInit();
}
}
I think this file is my problem
File display List of food
class List_food extends StatelessWidget {
final cartController = Get.put(CartController());
final productController = Get.put(ProductController());
final ProductController pro = Get.find();
List_food({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Obx(
() => Container(
child: ListView(
scrollDirection: Axis.vertical,
physics: const ScrollPhysics(),
shrinkWrap: true,
children: <Widget>[
Column(
children: List.generate(
productController.product.length,
(index) => Padding(
padding: const EdgeInsets.only(top: 15, bottom: 15.0),
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsScreen(
product: pro.product[index]),
));
},
child: Card(
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 15.0),
child: Container(
height: 120,
width: 120,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
pro.product[index].image),
fit: BoxFit.cover)),
),
),
Container(
width: 269,
child: Padding(
padding: const EdgeInsets.only(left: 15.0),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: <Widget>[
Text(
pro.product[index].title,
style: const TextStyle(
fontSize: 20,
color: Colors.black),
),
const SizedBox(
height: 5,
),
Text(
"Price " +
NumberFormat("#,###").format(
pro.product[index].price) +
" Bath",
style: const TextStyle(
fontSize: 18,
color:
Color.fromARGB(117, 0, 0, 0),
fontWeight: FontWeight.w400),
),
const SizedBox(
width: 20,
),
],
),
),
),
IconButton(
icon: LineIcon(
LineIcons.shoppingCartArrowDown,
size: 30,
),
onPressed: () {
cartController.addProdust(pro.product[index]);
},
),
const SizedBox(
width: 20,
),
],
),
),
),
)
),
),
], //
),
),
);
}
}
my full project is this : https://github.com/stm-seek/flutter-error-firebase.git
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
