没有为“ DocumentSnapshot”类型定义运算符“ []”

时间:2020-08-20 07:12:06

标签: flutter google-cloud-firestore

[这里是错误] 我已经在另一个类中创建了构造函数,但是它没有用。

这是代码...

  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'],
                );
              },
            );
    },
  ),
);
}
}

4 个答案:

答案 0 :(得分:8)

您正在使用^ 0.14.0。不建议直接调用[]

 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'],
                );
              },
            );
    },
  ),
);
}
}

答案 1 :(得分:5)

尝试一下,

  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'],
                );
              },
            );
    },
  ),
);
}
}

答案 2 :(得分:0)

I think first you should create a class for your products and write a method
for parsing data that comes from api.

class Product{
   String id:
   String name;
   String quantity;

 Product({this.id,this.name,this.quantity});

   factroy Product.fromJson(Map<String,dynamic> json) => Product(
      id : json['id'],
      name : json['name'],
      quantity : json['quantity']);
   }

  Create a list of product which you wanna parse your data.
  => List<Product> products = List<Product>(); //let say home page

  make a request to api and when the condition is fine for parsing data, assing
  snapshot data to the list that we've created under State<yourWidget>.

  body: StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance.collection("products").snapshots(),
      builder: (context, snapshot) {
  if(snapshot.connectionState == ConnectionState.done && snapshot.hasData) {
           return ListView.builder(
            itemCount: products.length,
            itemBuilder: (context, index) {
            return Product(
              id: products[index].id,
              isFavourite: products[index].name,
              imageUrl: products[index].quantity,
            );
          },
        );
         } else {
      return Center(child: CircularProgressIndicator());
          }

        },
      ),
    );
  }
}

答案 3 :(得分:0)

您必须将数据类型设置为字符串,以便Map最新的Firestore能够解决此问题。

Map<String, dynamic> data = user.data(); 
currentUser.name = data['name']; 

在模块窗口小部件的main.dart中

itemBuilder: (context, index) {
                DocumentSnapshot ds = snapshot.data.documents[index];
                Map<String, dynamic> data = ds.data();
                print(ds);
                return Container(
                  child: ListTile(
                    title: Text(
                      data['name'],
                    ),
                  ),
                );

在函数的模型类中,使用下面的链接

 List<Users> _brewListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.docs.map((doc) {
  Map<String, dynamic> data = doc.data();
  return Users(
    name: data['name'] ?? '',
    email: data['email'] ?? '',
  );
}).toList();

}