首页 > 解决方案 > 尝试从 Firestore 获取数据但收到此错误

问题描述

我正在尝试从 Firebase Firestore 获取数据并收到以下错误,相同的代码在一个月前与我的其他应用程序一起使用,如果您能提供帮助,那就太好了

类“列表”没有实例获取器“长度”。接收方:“_GrowableList”的实例(长度:2)尝试调用:长度

    return Scaffold(
                body: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('Sell')
              .orderBy('TS', descending: true)
              .snapshots(),
          builder: (context, snapshot) {
            if (snapshot.data == null)
              return Center(
                child: CircularProgressIndicator(
                  backgroundColor: Colors.red,
                  valueColor:
                      new AlwaysStoppedAnimation<Color>(Colors.cyan[400]),
                ),
              );
            return ListView.builder(
              itemCount: snapshot.data.documents.lenght,
              itemBuilder: (context, index) => SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Container(
                    height: 120,
                    width: MediaQuery.of(context).size.width,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(7),
                        boxShadow: [
                          BoxShadow(
                              color: Colors.grey[400],
                              spreadRadius: 2,
                              blurRadius: 3)
                        ]),
                    child: Row(
                      children: [
                        Container(
                          width: 120,
                          height: 120,
                          child:ClipRRect(
                                 child: Image.network(
                                    snapshot.data.documents[index]
                                        .get('image 1 Url'),
                                    fit: BoxFit.cover,
                                  ),
                                )),
                        SizedBox(width: 20),
                        Column(
                          children: [
                            Text(
                              snapshot.data.documents[index].get('name'),
                              style: TextStyle(
                                color: Colors.red,
                                fontSize: 20,
                                fontFamily: 'font',
                              ),
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        ));
  }
}

标签: firebaseflutterdartgoogle-cloud-firestore

解决方案


更改lenghtlength

我认为这是一个错字。如果你想知道为什么length。那是因为您正在尝试获取长度snapshot.data.documents并将其初始化itemCount以允许列表循环您的项目。我希望这个解决方案对你有用。


推荐阅读