【问题标题】:Flutter How to get length of a iterable .toList() list?Flutter 如何获取可迭代的 .toList() 列表的长度?
【发布时间】:2021-11-12 09:27:51
【问题描述】:

我想显示一个带有 FirebaseFirestore 卡片的 ListView。另外,我想显示一共有多少张卡片:

int Length = 0; //?

如何获取可迭代 .toList() 列表的长度?

     Column(
         children: snapshot.data!.docs
             .map(
                (doc) => 
                   Card(
                    title: doc["title"],)
                 .toList(
                growable: true,
             ),
          ),
       ),

【问题讨论】:

  • 只需在其末尾添加 .length:.toList().length;
  • 错误:参数类型'int'不能分配给参数类型'List'。
  • 在构建widget树之前获取长度,然后在树中使用。

标签: android flutter dart listview


【解决方案1】:
Column(
         ...snapshot.data!.docs
             .asMap().map((index, value) => MapEntry(index, Card(
                    title: value["title"],)))
                 .values.toList(
                growable: true,
             ),
          ),
      

【讨论】:

  • 对您有帮助吗?
  • 你能告诉我如何设置卡片数量的 int Length 的状态吗?
  • 为什么需要它?您的小部件将根据长度构建
  • 这就是我想要基于生成的列表长度的长度?或者这不起作用?
  • 这个方法包括itemCount,如果你想得到长度使用snapshot.data!.docs.length
猜你喜欢
  • 2021-08-05
  • 2022-01-23
  • 2018-02-02
  • 2022-12-13
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 2017-12-30
  • 2017-10-15
相关资源
最近更新 更多