颤振使目录索引 [英] Flutter making a directory index
问题描述
我面临着创建带有字母索引的目录的挑战.
Im facing challenge creating a directory with alphabet index.
我有List< Map< String,dynamic>>如下的商店:
I have List<Map<String, dynamic>> of shops like below:
var shops = [
{'name':'Thrifty','products':'cars', 'delivery':'no'},
{'name':'Pizza Express','products':'pizza', 'delivery':'yes'},
{'name':'Fastmart','products':'household items', 'delivery':'yes'}
];
,并希望呈现以字母分隔的卡片列表,如下所示:
and would like to render list of cards separated with alphabet like below:
我正在使用Cloud Firestore来获取我的数据,如果有一种直接查询数据库或在客户端完成数据库的方法,请告诉我.
I'm using cloud firestore to get my data, if there is a way to query the database directly or have it done client-side please let me know.
感谢任何帮助和指导!
推荐答案
我可以给你个主意.尝试这样的事情,
I could give you an idea. Try something like this,
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
来按字母顺序存储对象.
First of all, we declare a Map
to store the objects letter-wise.
然后,列出可用字母并对其进行排序以用于显示.
Then, make a list of available letters and sort them for display purposes.
然后,针对不同的用例进行遍历.
Then, iterate through it for different use cases.
希望能满足您的情况!
这篇关于颤振使目录索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!