Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The operator '[]' isn't defined for the type 'DocumentSnapshot'

[Here is the error] I've created constructors in another class but it ain't working.

Here is the code...

  body: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance.collection("products").snapshots(),
    builder: (context, snapshot) {
      return !snapshot.hasData
          ? Center(child: CircularProgressIndicator())
          : ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (context, index) {
                DocumentSnapshot data = snapshot.data.docs[index];
                return ProductItem(
                  documentSnapshot: data,
                  id: data.id,
                  isFavourite: data['isFavourite'],
                  imageUrl: data['imageUrl'],
                  productName: data['productName'],
                  productPrice: data['productPrice'],
                );
              },
            );
    },
  ),
);
}
}
like image 405
Robel Avatar asked Aug 20 '20 07:08

Robel


2 Answers

You are using ^0.14.0. Calling [] directly is deprecated

 body: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance.collection("products").snapshots(),
    builder: (context, snapshot) {
      return !snapshot.hasData
          ? Center(child: CircularProgressIndicator())
          : ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (context, index) {
                DocumentSnapshot data = snapshot.data.docs[index];
                return ProductItem(
                  documentSnapshot: data,
                  id: data.id,
                  isFavourite: data.data()['isFavourite'],
                  imageUrl: data.data()['imageUrl'],
                  productName: data.data()['productName'],
                  productPrice: data.data()['productPrice'],
                );
              },
            );
    },
  ),
);
}
}
like image 96
Siddharth jha Avatar answered Sep 28 '22 00:09

Siddharth jha


Try this,

  body: StreamBuilder<QuerySnapshot>(
    stream: FirebaseFirestore.instance.collection("products").snapshots(),
    builder: (context, snapshot) {
      return !snapshot.hasData
          ? Center(child: CircularProgressIndicator())
          : ListView.builder(
              itemCount: snapshot.data.docs.length,
              itemBuilder: (context, index) {
                DocumentSnapshot data = snapshot.data[index];
Map getDocs = data.data; 
                return ProductItem(
                  documentSnapshot: data,
                  id: data.id,
                  isFavourite: getDocs['isFavourite'],
                  imageUrl: getDocs['imageUrl'],
                  productName:getDocs['productName'],
                  productPrice: getDocs['productPrice'],
                );
              },
            );
    },
  ),
);
}
}
like image 37
Madhavam Avatar answered Sep 28 '22 00:09

Madhavam