【问题标题】:Length of snapshot for List View builder in flutter[Fixed]Flutter 中列表视图构建器的快照长度 [已修复]
【发布时间】:2021-10-01 20:21:02
【问题描述】:

我需要在 Flutter 上实现一个 ListView,并且我将 snapshot.data.length 作为 itemCount 的参数传递

return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(                            
snapshot.data[index].data["Identificacao"],...

然后我得到一个错误:

I/flutter ( 4647): Class 'List<DocumentSnapshot>' has no instance getter 'length'.
I/flutter ( 4647): Receiver: Instance(length:1) of '_GrowableList'
I/flutter ( 4647): Tried calling: length

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies flutter-web flutter-test


    【解决方案1】:

    很容易解决

    solved the problem by replacing StreamBuilder<Object> with StreamBuilder<QuerySnapshot>. by 
    default the StreamBuilder comes in this form StreamBuilder<Object>
    

    这是一个例子

    StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance
              .collection('chats/hgjgjgjGW/messages')
              .snapshots(),
          builder: (cnxt, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (ctx, i) => Container(
                  padding: EdgeInsets.all(10),
                  child: Text(snapshot.data!.docs[i]['text']),
                ),
              );
            }
            return Center(
              child: Text('error'),
            );
          }),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2021-09-20
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多