Home > Mobile >  how to fetch data from firebase flutter?
how to fetch data from firebase flutter?

Time:09-30

Here is my model and I wanna store this data in a list ,using getx .null safety method is on.

    var productlist = <Productmodel>[].obs;


  Productmodel.frommap( DocumentSnapshot snapshot) {
        id = snapshot['id'];
        productname = snapshot['name'];
        price = snapshot['price'];
        image = snapshot['image'];
        brand = snapshot['brand'];
      }

data in firebase database is shown here

CodePudding user response:

In this way, you can access the data by giving the names collection and field.

Example

CodePudding user response:

try this method...

Future<void> getAllData() async {
   List<Productmodel> productModelList = [];

   var data = await FirebaseFirestore.instance.collection("product").doc('CWvaDGh5ZaCznGtLnq6A').get();
   List dataList = data.data()?['product'];
   
   for (var element in dataList) {
     productModelList.add(Productmodel.fromJson(element));
   }

  print('productModelList : ${productModelList.toList()}');
}
  • Related