【发布时间】:2018-12-18 18:11:27
【问题描述】:
在我在 FireStore 上的文档中,每个文档都有一个字符串列表。当我在应用程序中显示文档时,我想按字母顺序对它们进行排序。我正在尝试的方法不起作用。
var words = document['list'].cast<String>();
words.sort(); // Outputs 'null'
在调试器中检查时,当我投射列表时,对象的类型为 CastList,但我找不到任何关于此的信息,并且尝试创建具有该声明类型的对象告诉我它是一个未定义的类。所以我尝试指定我想要的类:
List<String> words = document['list'].cast<String>();
但是当我尝试排序时它仍然输出null。
我正在获取lists 中的所有文档,并在列表视图中显示它们中的每一个。
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);
});
},
);
}
【问题讨论】:
-
你能告诉我们你来自firestore的数据吗? (收藏/文件)
-
@diegoveloper 添加了一张图片。
标签: dart flutter google-cloud-firestore