【问题标题】:Length of snapshot for Listview on FlutterFlutter 上 Listview 的快照长度
【发布时间】:2019-03-27 03:43:29
【问题描述】:

我需要在 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

但是我看到的许多教程中都使用了这些 sintax。我试过用:

snapshot.data.documents.length;

但结果是一样的。请帮帮我!

【问题讨论】:

  • 这个副本是+粘贴还是lenght在您的源代码中拼写错误(两次)?编辑:再看一遍,看起来像是基于堆栈跟踪的错字。
  • 我实际上在这里写错了问题。但甚至没有建议(Ctrl + 空格键)。因为它是一个列表,我想必须有一种方法可以知道大小,以便您可以通过循环等方式遍历它...
  • 快一周了,没有任何反应,哇...我很高兴我选择了一种由 Google 维护的流行语言来开发我的第一个应用程序
  • 抛开抨击,snapshot 是什么?你在学习什么教程?

标签: listview flutter snapshot


【解决方案1】:

也许snapshot.documents.length

【讨论】:

  • 没有这样的东西。
【解决方案2】:

我用它来映射和递增以获得快照的长度:

int snapshotLength = 0;

snapshot.data.documents.map<YourGenericType>((document){
 snapshotLength++;
 print("Snapshot length: $snapshotLength");
}

【讨论】:

    【解决方案3】:

    在 Flutter 中使用 StreamBuilder 小部件。

    StreamBuilder(
            stream: Firestore.instance
                    .collection('followers')
                    .document(<DocID>)
                    .collection('<COLLECTION>')
                    .snapshots(),
            builder: (context, snapshot) {
                    QuerySnapshot values = snapshot.data;
                    print(values.length);
           },
    );
    

    【讨论】:

      【解决方案4】:

      如果您使用的是 StreamBuilder,那么查找数据长度的方法就不是这样了。

      snapshot.data.length
      

      由于您询问快照实例的长度,因此无法正常工作,因此您将遇到 No such methodNo such class 错误

      所以你应该做的是。

      snapshot.data.snapshot.value.length
      

      我给你举个例子

      StreamBuilder(
          stream: FirebaseDatabase.instance
                    .reference()
                    .child("users")
                    .orderByChild('firstName')
                    .limitToFirst(20)
                    .onValue,
          builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return ListView.builder(
                      itemCount: snapshot.data.snapshot.value.lenght,//Here you can see that I will get the count of my data
                        itemBuilder: (context, int) {
                        //perform the task you want to do here
                          return Text("Item count ${int}");
                        });
                  } else {
                    return Container();
                  }
            },
        ),
      

      你也可以看看这个关于stream和futurbuilder有什么区别https://stackoverflow.com/a/50844913/9949983的答案

      【讨论】:

        【解决方案5】:

        只尝试: snapshot.data.documents.length 不建议在 vscode ide dart 插件中使用,但它会起作用。

        【讨论】:

          【解决方案6】:

          通过替换解决了问题

          StreamBuilder<Object> 
          

          StreamBuilder<QuerySnapshot> 
          

          默认情况下,StreamBuilder 采用这种形式 StreamBuilder

          这将 100% 有效

          【讨论】:

            猜你喜欢
            • 2020-08-17
            • 2021-10-01
            • 1970-01-01
            • 2021-08-06
            • 2020-04-12
            • 2020-07-20
            • 1970-01-01
            • 2021-07-07
            • 2019-03-22
            相关资源
            最近更新 更多