![](/img/trans.png)
[英]Unhandled Exception NoSuchMethodError: The method '[]' was called on null in flutter
[英]Flutter Unhandled Exception: NoSuchMethodError: The method 'then' was called on null
發生異常。 NoSuchMethodError (NoSuchMethodError: The method 'then' was called on null. Receiver: null 嘗試調用: then(Closure: (dynamic) => Null))朋友們,我收到了上面提到的錯誤。 有誰知道如何解決這個問題? 我正在嘗試通過firebase進行用戶搜索。有人知道其他方法嗎?
class _DiscoverScreen extends State<DiscoverScreen> {
DatabaseMethods databaseMethods = DatabaseMethods();
TextEditingController searchEditingController = TextEditingController();
QuerySnapshot? searchResultSnapshot;
@override
void dispose() {
searchEditingController.dispose();
super.dispose();
}
initiateSearch() async {
if (searchEditingController.text.isNotEmpty) {
await databaseMethods
.searchByName(searchEditingController.text)
.then((value) {
value.docs.forEach(
(result) {
searchResultSnapshot = value;
print("$searchResultSnapshot");
},
);
});
}
}
Widget userList() {
return searchResultSnapshot != null
? ListView.builder(
shrinkWrap: true,
itemCount: searchResultSnapshot!.docs.length,
itemBuilder: (context, index) {
return SearchTile(
name: searchResultSnapshot!.docs[index].get("name"),
username: searchResultSnapshot!.docs[index].get("username"),
image: searchResultSnapshot!.docs[index].get("photoUrl"),
);
})
: Container();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
centerTitle: false,
title: Text("Keşfet"),
backgroundColor: Colors.grey.shade700,
),
body: Column(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 16),
child: Row(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search username",
hintStyle: TextStyle(
color: Colors.black,
fontSize: 16,
),
),
style: TextStyle(
color: Colors.black,
fontSize: 20,
),
controller: searchEditingController,
),
),
IconButton(
onPressed: () {
initiateSearch();
},
icon: ImageIcon(
AssetImage(
"assets/icons/searchicon.png",
),
size: 20,
))
],
),
),
userList(),
],
),
);
}
}
searchByName 代碼 =
searchByName(String username) {
FirebaseFirestore.instance
.collection("users")
.where("username", isEqualTo: username)
.get();
}
似乎searchByName
正在返回 null。 要么使用?
運算符或提供包含searchByName
函數的代碼示例以查找問題。
我們主要使用未來的方法
使這個數據庫方法未來類型
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.