Flutter 从firestore获取某些文档数据以填充下拉列表
我正试图用firestore数据填充我的下拉列表。我希望尽可能地轻,而不需要任何流生成器排序或任何其他我不需要的东西。 我已经在我的服务中创建了对firestore的可重用调用,以便只获取我想要的文档Flutter 从firestore获取某些文档数据以填充下拉列表,flutter,dart,google-cloud-firestore,Flutter,Dart,Google Cloud Firestore,我正试图用firestore数据填充我的下拉列表。我希望尽可能地轻,而不需要任何流生成器排序或任何其他我不需要的东西。 我已经在我的服务中创建了对firestore的可重用调用,以便只获取我想要的文档 Future<List<Map<String, dynamic>>> getCollections<T>(String path) async { final data = await Firestore.instance.collect
Future<List<Map<String, dynamic>>> getCollections<T>(String path) async {
final data = await Firestore.instance.collection(path).getDocuments();
final result = data.documents.map((doc) => doc.data).toList();
return result;
}
Future getCollections(字符串路径)异步{
final data=wait Firestore.instance.collection(path.getDocuments();
最终结果=data.documents.map((doc)=>doc.data.toList();
返回结果;
}
我在数据库中使用它来获取特定路径上的集合
Future<List<Map<String, dynamic>>> brandStream() =>
_service.getCollections('all_brands');
Future brandStream()=>
_服务。getCollections(“所有品牌”);
然后现在在我的按钮中调用它来打印数据
onPressed: () async {
final database =
Provider.of<Database>(context, listen: false);
var r = await database.brandStream();
print(r);
}
onPressed:()异步{
最终数据库=
Provider.of(上下文,listen:false);
var r=await database.brandStream();
印刷品(r);
}
这一切都是可行的,但很明显,它也提取了我不感兴趣的数据。我只需要从每个集合中获取名称和图像url,但我会获取该集合中的所有其他内容。最好的方法是什么?我不知道如何将数据添加到只包含名称和url的模型类中(从我前面的问题开始)您必须像这样在服务文件中包括t builder
Future<List<T>> getCollections<T>(
{String path,
@required
T builder(Map<String, dynamic> data, String documentID)}) async {
final data = await Firestore.instance.collection(path).getDocuments();
final result =
data.documents.map((doc) => builder(doc.data, doc.documentID)).toList();
return result;
}
class Brand {
String logo, name, bid;
Brand({this.logo, this.name, this.bid});
factory Brand.fromMap(Map<String, dynamic> brandData, String documentID) {
if (brandData == null) {
return null;
}
final String logo = brandData['logo'];
final String name = brandData['logo'];
return Brand(logo: logo, name: name, bid: documentID);
}
}
未来的getCollections(
{字符串路径,
@必需的
T生成器(映射数据,字符串documentID)})异步{
final data=wait Firestore.instance.collection(path.getDocuments();
最终结果=
data.documents.map((doc)=>builder(doc.data,doc.documentID)).toList();
返回结果;
}
然后在数据库文件中,使用生成器将数据添加到模型类中
@override
Future<List<Brand>> brandStream() => _service.getCollections(
path: 'tool_bank', builder: (data, id) => Brand.fromMap(data, id));
@覆盖
Future brandStream()=>\u service.getCollections(
路径:'tool_bank',builder:(数据,id)=>Brand.fromMap(数据,id));
模型类应该是这样的
Future<List<T>> getCollections<T>(
{String path,
@required
T builder(Map<String, dynamic> data, String documentID)}) async {
final data = await Firestore.instance.collection(path).getDocuments();
final result =
data.documents.map((doc) => builder(doc.data, doc.documentID)).toList();
return result;
}
class Brand {
String logo, name, bid;
Brand({this.logo, this.name, this.bid});
factory Brand.fromMap(Map<String, dynamic> brandData, String documentID) {
if (brandData == null) {
return null;
}
final String logo = brandData['logo'];
final String name = brandData['logo'];
return Brand(logo: logo, name: name, bid: documentID);
}
}
class品牌{
字符串标志,名称,出价;
品牌({this.logo,this.name,this.bid});
factory Brand.fromMap(地图品牌数据,字符串文档ID){
如果(brandData==null){
返回null;
}
最终字符串logo=brandData['logo'];
最终字符串名称=brandData['logo'];
退货品牌(logo:logo,name:name,bid:documentID);
}
}
然后你就可以像使用它一样使用它了谢谢。。这就是我要找的