[英]Flutter Unhandled Exception: NoSuchMethodError: The method 'then' was called on null
Exception has occurred.发生异常。 NoSuchMethodError (NoSuchMethodError: The method 'then' was called on null. Receiver: null Tried calling: then(Closure: (dynamic) => Null)) Friends, I am getting the error I mentioned above.
NoSuchMethodError (NoSuchMethodError: The method 'then' was called on null. Receiver: null 尝试调用: then(Closure: (dynamic) => Null))朋友们,我收到了上面提到的错误。 Does anyone know how to solve this?
有谁知道如何解决这个问题? I'm trying to user search via firebase.Does anyone know any other way to do this?
我正在尝试通过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 Codes = searchByName 代码 =
searchByName(String username) {
FirebaseFirestore.instance
.collection("users")
.where("username", isEqualTo: username)
.get();
}
It seems like searchByName
is returning null.似乎
searchByName
正在返回 null。 Either do a null check using the ?
要么使用
?
operator or provide a code sample with the code containing searchByName
function in order to find the problem.运算符或提供包含
searchByName
函数的代码示例以查找问题。
we use method then with future mostly我们主要使用未来的方法
make this databaseMethods Future type使这个数据库方法未来类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.