简体   繁体   English

初始化未来<querysnapshot>在 setState Flutter</querysnapshot>

[英]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.

相关问题 Flutter 从 QuerySnapshot 转换为 Future <list<map<dynamic, dynamic> >> </list<map<dynamic,> - Flutter Convert from QuerySnapshot to a Future <List<Map<dynamic, dynamic>>> StreamProvider 的初始数据<querysnapshot> .value flutter</querysnapshot> - initial data for StreamProvider<QuerySnapshot>.value flutter Flutter Firebase 在同一小部件中返回 Querysnapshot 和 DocumentSnapshot - Flutter Firebase return a Querysnapshot and DocumentSnapshot in the same widget 如何在列表视图生成器中使用 querySnapshot? (扑) - How to use querySnapshot in a listview builder? (flutter) 使用 Stream 检索数据<querysnapshot>在 Firebase 和 Flutter</querysnapshot> - Retrieving Data with Stream<QuerySnapshot> in Firebase with Flutter 'Future 类型的值<querysnapshot<map<string, dynamic> &gt;&gt;' 不能分配给“QuerySnapshot”类型的变量<object?> ' </object?></querysnapshot<map<string,> - A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>' Flutter firestore QuerySnapshot 在 Android 中没有 getter“文档”的实例 - Flutter firestore QuerySnapshot has no instance of getter 'documents' in Android flutter firebase querysnapshot: dart代码中不区分大小写的方法 - flutter firebase querysnapshot: case insensitive method in dart code flutter 中带有 for 循环的 Future Builder - Future Builder with for loop in flutter Firestore Flutter:获取 querySnapshot.length 并设置新文档 - Firestore Flutter : get querySnapshot.length & set new doc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM