[英]Flutter - Error in BuildSuggestions (Search Delegate) using Firestore instance
我對 buildSuggestions 有一些疑問。 當我鍵入內容時,它顯示“Instance of 'DocumentSnapshot'”而不是文檔名稱,如下圖所示。
這是我的代碼:
@override
Widget buildSuggestions(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance.collection("anuncios").where("caseSearch", arrayContains: query).snapshots(),
builder: (context, snapshot) {
if (query.isNotEmpty);
List<DocumentSnapshot> results = snapshot.data.documents.where(
(DocumentSnapshot a) => a.data["titulo"].toString().contains(query)).toList();
return ListView.builder(
itemCount: results.length,
itemBuilder: (context, index){
return ListTile(
onTap: (){
close(context, results[index].toString());
},
title: Text(results[index].toString()),
);
}
);
},
);
}
您的代碼的問題是您的snapshot.data.documents.where(...)
返回一個QuerySnapshot
object,無法通過在其后執行.ToList()
將其轉換為DocumentSnapshot
列表。
您需要做的是使用QuerySnapshot
object 的docs
屬性,這正是您想要的,一個List<DocumentSnapshot>
。 因此,將其應用於您的代碼,您只需為此替換上述行:
List<DocumentSnapshot> results = (snapshot.data.documents.where((DocumentSnapshot a) => a.data["titulo"].toString().contains(query))).docs;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.