dart - 如何从 FireStore 中排序列表

标签 dart flutter google-cloud-firestore

在我在 FireStore 上的文档中,每个文档都有一个字符串列表。当我在应用程序中显示文档时,我想按字母顺序对它们进行排序。我正在尝试的方法不起作用。

var words = document['list'].cast<String>();
words.sort(); // Outputs 'null'

在调试器中检查时,当我转换列表时,对象的类型为 CastList,但我找不到任何关于此的信息,并尝试创建一个声明的对象type 告诉我这是一个未定义的类。因此,我尝试指定我希望它成为的类:

List<String> words = document['list'].cast<String>();

但是当我尝试排序时它仍然输出 null

我的收藏是这样的 Collection example

我正在获取 lists 中的所有文档,并在 listView 中显示它们中的每一个。

StreamBuilder(
  stream: Firestore.instance.collection('lists').orderBy('releases').snapshots,
  builder: (context, snapshot) {
    if (!snapshot.hasData)
      return const Center(child: Text('Loading...'));

      return ListView.builder(
        itemCount: snapshot.data.documents.length,
        itemBuilder: (context, index) =>
            _buildRow(context, snapshot.data.documents[index], index),
      );
    },
)

  Widget _buildRow(BuildContext context, DocumentSnapshot document, int index) {
    var words = document['list'].cast<String>();
    var wordsString = words.toString();
    wordsString = wordsString.substring(1, wordsString.length - 1);

    return CheckboxListTile(
      title: Text(
        document['name'],
        style: _largerTextStyle,
      ),
      subtitle: Text(
        wordsString,
        style: _textStyle,
      ),
      value: _selectedIndices.contains(index),
      onChanged: (bool value) {
        setState(() {
          if (value) _selectedIndices.add(index);
          else _selectedIndices.remove(index);
        });
      },
    );
  }

最佳答案

它应该可以工作,不需要调用 cast

编辑: 我想你忘了提取数据。

List words = document.data['list'];
words.sort();

关于dart - 如何从 FireStore 中排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53838841/

相关文章:

dart - 正确杀死新产生的分离株

function - dart 如何返回 future

flutter - 在 Flutter 中动态生成 widget

android - 在Android Studio中的Flutter上运行模拟器时出错

firebase - 找不到一些要求的文件。

Dart eclipse-plugin 找不到更新站点

json - 如何访问 Dart shelf post body 参数?

java - Flutter build apk 命令抛出错误并且未构建 apk

javascript - 从触发器内在云存储桶中创建文件

firebase - 没有创建 Firebase App '[DEFAULT]' - 在 Flutter 和 Firebase 中调用 Firebase.initializeApp()