我面临着创建带有字母索引的目录的挑战。
我有如下商店的List
var shops = [
{'name':'Thrifty','products':'cars', 'delivery':'no'},
{'name':'Pizza Express','products':'pizza', 'delivery':'yes'},
{'name':'Fastmart','products':'household items', 'delivery':'yes'}
];
,并希望呈现以字母分隔的卡片列表,如下所示:
我正在使用Cloud Firestore来获取我的数据,如果有一种直接查询数据库或在客户端完成数据库的方法,请告诉我。
感谢任何帮助和指导!
答案 0 :(得分:0)
我可以给你个主意。尝试这样的事情,
var shops = [
{'name':'Thrifty','products':'cars', 'delivery':'no'},
{'name':'Pizza Express','products':'pizza', 'delivery':'yes'},
{'name':'Fastmart','products':'household items', 'delivery':'yes'}
];
var indexedShops = {};
shops.forEach((item) {
String letter = item['name'][0];
if(indexedShops[letter] != null){
indexedShops[letter] += [item];
}else{
indexedShops[letter] = [item];
}
});
print(indexedShops);
// {T: [{name: Thrifty, products: cars, delivery: no}], P: [{name: Pizza Express, products: pizza, delivery: yes}], F: [{name: Fastmart, products: household items, delivery: yes}]}
var availLetters = indexedShops.keys.toList();
availLetters.sort();
availLetters.forEach((e){
print(indexedShops[e]);
});
// [{name: Fastmart, products: household items, delivery: yes}]
// [{name: Pizza Express, products: pizza, delivery: yes}]
// [{name: Thrifty, products: cars, delivery: no}]
首先,我们声明一个Map
来按字母顺序存储对象。
然后,列出可用字母并将其排序以供显示。
然后,针对不同的用例进行遍历。
希望能满足您的情况!