【问题标题】:flutter firebase get all documents' field of type arrayflutter firebase获取所有文档的数组类型字段
【发布时间】:2021-05-28 21:53:13
【问题描述】:

我有一个颤振项目,我在其中显示产品及其信息,但在 firebase 中,我将图像类型从字符串转换为数组,并且由于类型更改而遇到问题。在另一堂课中,我有相同的示例,但仅限于一个文档,不适用于我的案例。

void getImage() async {
    await FirebaseFirestore.instance
        .collection('Product')
        .doc(docID)
        .get()
        .then((document) {
      imageLists = document.data()['image'];
    });
  }

有没有办法让这段代码接受数组类型?

StreamBuilder<QuerySnapshot>(
              stream: product.snapshots(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return Flexible(
                    child: GridView.builder(
                        itemCount: snapshot.data.docs.length,
                        gridDelegate:
                            SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                        ),
                        itemBuilder: (context, index) {
                          return Padding(
                            padding: const EdgeInsets.fromLTRB(8, 16, 8, 0),
                            child: Image.network(
                              // here
                              snapshot.data.docs[index].get('image'),
                              width: 150,
                              height: 120,
                            ),
                          );                             
                        }),
                  );
                }
              }),
        

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    好吧,我刚刚找到了一个解决方案替换这段代码:

    snapshot.data.docs[index].get('image'),
    

    有了这个

    snapshot.data.docs[index]['image'][0],
    

    问题是第一个代码只接受字符串类型,通过在数组括号中添加字段,它知道类型是数组并为数组索引添加额外的括号。

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 1970-01-01
      • 2021-03-01
      • 2020-09-20
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2021-08-31
      相关资源
      最近更新 更多