use StreamBuilder to fetch Firestore document which contains list of map in flutter

I want read data from my firestore collection that has the format below

enter image description here

This is the class below. Don't Know how to use streamBuilder to read the documents. I also want a situation where I can pass a selected order to the next page. Thanks in advance.

class OrderModel2 {
  final String uid;
  final int subTotal;
  final String address;
  final String address2;
  final String zipCode;
  final String phoneNumber;
  final dynamic timeCreated;
  final String status;
  final List<OrdersList> orders;

  OrderModel2(
      {this.timeCreated,
      this.status,
      this.subTotal,
      this.phoneNumber,
      this.zipCode,
      this.uid,
      this.address2,
      this.orders,
      this.address});

  OrderModel2.fromMap(Map<String, dynamic> data, String uid)
      : subTotal = data['subTotal'],
        address = data['address'],
        zipCode = data['zipCode'],
        phoneNumber = data['phoneNumber'],
        address2 = data['address2'],
        timeCreated = data['timeCreated'],
        status = data['status'],
        orders = data['orders'],
        uid = uid;
}

class OrdersList {
  final String productName;
  final String selected;
  final String image;
  final int price;
  final String category;

  OrdersList(
      {this.productName, this.selected, this.image, this.price, this.category});
  OrdersList.fromMap(Map<dynamic, dynamic> data)
      : productName = data['productName'],
        selected = data['selected'],
        image = data['image1'],
        price = data['newPrice'],
        category = data['category'];
}

Upvotes: 1

Views: 1082

Answers (1)

Gene
Gene

Reputation: 392

This is how you would use SteamBuilder to access the documents within the Orders Collection.

StreamBuilder<QuerySnapshot>(
  stream: Firestore.instance.collection('Orders').snapshots(),
  builder: (
    BuildContext context,
    AsyncSnapshot<QuerySnapshot> snapshot,
    ) {
  if (!snapshot.hasData) return new Text('Loading...');
  body: StreamBuilder<QuerySnapshot>(
    stream: Firestore.instance.collection("products").snapshots(),
    builder: (context, snapshot) {
      return !snapshot.hasData
          ? Center(child: CircularProgressIndicator())
          : ListView.builder(
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) {
                DocumentSnapshot data = snapshot.data.documents[index];
                return YourObject(
                  documentSnapshot: data,
                  paramI: data.documentID,
                  paramField: data['field'],
                );
              },
            );

Definitely read this to get a whole picture of what's happening ! https://medium.com/flutterdevs/using-firebase-firestore-in-flutter-b0ea2c62bc7

Also here is a StackOverflow about adding a searchfield also! Adding SearchField to StreamBuilder Reading from Firestore

Upvotes: 3

Related Questions