[英]initialize Future<QuerySnapshot> in the setState Flutter
i have a function called handleSearch, inside i make querysnapshot from user document from firestore and i want to input it into setState.我有一个名为 handleSearch 的 function,在里面我从 firestore 的用户文档中制作查询快照,我想将它输入到 setState 中。
this is my code这是我的代码
final usersRef = FirebaseFirestore.instance.collection('users');
late Future<QuerySnapshot> searchResultsFuture; // this is the line of error
handleSearch(String query) {
Future<QuerySnapshot> users =
usersRef.where("name", isGreaterThanOrEqualTo: query).get();
setState(() {
searchResultsFuture = users;
});
}
this is the textFormField这是 textFormField
AppBar(
backgroundColor: Colors.white,
title: TextFormField(
controller: searchController,
decoration: InputDecoration(
hintText: "Search for user...",
filled: true,
prefixIcon: Icon(
Icons.account_box,
size: 28.0,
),
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: clearSearch,
),
),
onFieldSubmitted: handleSearch,
),
);
The error suggest me to add late modifier错误建议我添加 late 修饰符
but after i add it但在我添加它之后
any suggestion what should i do?有什么建议我该怎么办?
When you use late
keyword, it is expected to be initialized in the initState({})
method.当你使用
late
关键字时,它应该在initState({})
方法中被初始化。
or要么
You can set the value to nullable type您可以将值设置为可空类型
Future<QuerySnapshot>? searchResultsFuture;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.