如何从FireStore排序列表

科迪·哈尼斯

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

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

在调试器中检查时,当我投射列表时,该对象是类型的CastList,但是我找不到关于此的任何信息,尝试创建具有该声明类型的对象会告诉我这是一个未定义的类。因此,然后我尝试指定我想要的类:

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

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

我的收藏看起来像这样 收集范例

我将所有文档放入其中,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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章