简体   繁体   English

在 FutureBuilder 中使用 RefreshIndicator(QuerySnapshot 问题)

[英]Using RefreshIndicator with FutureBuilder (QuerySnapshot issue)

Im trying to implement the pull to refresh action, on a future builder using refresh indicator widget.我正在尝试使用刷新指示器小部件在未来的构建器上实现拉动刷新操作。 I've manage to get it to load correctly but when i try to refresh the list by pulling to refresh, cant get pass the issue im currently facing.我设法让它正确加载,但是当我尝试通过拉动刷新来刷新列表时,无法解决我目前面临的问题。

1. 1.

late Future<QuerySnapshot> postsList;
  1. Initialize the data初始化数据
void initState() {
  super.initState();

  postsList = loadPostsList();
}
  1. load data from firestore function从 Firestore 函数加载数据
Future<QuerySnapshot> loadPostsList() async {
  print('Refreshing Post List...');
  keyRefresh.currentState?.show();

  final QuerySnapshot data = await FirebaseFirestore.instance
      .collection('Posts')
      .orderBy('createdAt', descending: true)
      .get();

  return data;
}
  1. Now this is where im stuck, i want add setState(() => this.postsList = data);现在这是我卡住的地方,我想添加setState(() => this.postsList = data); into the loadPostsList() function like the following:进入 loadPostsList() 函数,如下所示:
Future<QuerySnapshot> loadPostsList() async {
  print('Refreshing Post List...');
  keyRefresh.currentState?.show();

  final QuerySnapshot data = await FirebaseFirestore.instance
      .collection('Posts')
      .orderBy('createdAt', descending: true)
      .get();

  // HERE
  setState(() => this.postsList = data);

  return data;
}

but getting this error:但收到此错误:

A value of type 'QuerySnapshot<Object?>' can't be assigned to a variable of type 'Future<QuerySnapshot<Object?>>'.不能将“QuerySnapshot<Object?>”类型的值分配给“Future<QuerySnapshot<Object?>>”类型的变量。

Any help is appreciate as im new to flutter and still learning :), below is the full code:感谢任何帮助,因为我是新手,并且仍在学习 :),以下是完整代码:

final keyRefresh = GlobalKey<RefreshIndicatorState>();
late Future<QuerySnapshot> postsList;

@override
void initState() {
  super.initState();

  postsList = loadPostsList();
}

Future<QuerySnapshot> loadPostsList() async {
  print('Refreshing Post List...');
  keyRefresh.currentState?.show();

  final QuerySnapshot data = await FirebaseFirestore.instance
      .collection('Posts')
      .orderBy('createdAt', descending: true)
      .get();

  return data;
}

FutureBuilder<QuerySnapshot>(
  future: postsList,
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      final List<DocumentSnapshot> documents =snapshot.data!.docs;

      return RefreshIndicator(
        key: keyRefresh,
        color: Colors.blue,
        onRefresh: loadPostsList,
        child: SingleChildScrollView(
          physics: AlwaysScrollableScrollPhysics(),
          child: Column(
            children: documents.map((doc) => new PostCard(
              author: doc['author'],
              title: doc['title'],
              description: doc['desc'],
            ),
          ).toList(),
        ),
      );
    } else if (snapshot.hasError) {
      return Text('Error!');
    } else {
      return LoadingWidget();
    }
  },
),

The error is because you defined data as type QuerySnapshot and are trying to assign it to postsList , which you defined as type Future<QuerySnapshot> .错误是因为您将data定义为QuerySnapshot类型,并试图将其分配给postsList ,您将其定义为Future<QuerySnapshot>类型。 Obviously, they are different types so you can't assign one to the other.显然,它们是不同的类型,因此您不能将一种分配给另一种。

Don't call setState from inside loadPostsList .不要从loadPostsList内部调用setState Use something like:使用类似的东西:

onRefresh: () async {
  setState(() {
    postsList = loadPostsList();
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Getx 在 Listview.Builder 中实现 RefreshIndicator? - How can I implement RefreshIndicator in Listview.Builder using Getx? Flutter Listview在使用FutureBuilder加载缩略图时冻结 - Flutter Listview freezes while it is loading thumbnail images using FutureBuilder 如何避免对使用带有 firestore 的 snapshotListener 删除的消息调用 onEvent(QuerySnapshot snapshots, FirestoreException error)? - How to avoid call onEvent(QuerySnapshot snapshots, FirestoreException error) for message deleted using the snapshotListener with firestore? Flutter:如何将 RefreshIndicator 与 SliverAppBar 一起使用 - Flutter: How to use RefreshIndicator with SliverAppBar QuerySnapshot 优化了 firebase - QuerySnapshot was optimized out firebase Flutter RefreshIndicator 不适用于 TabBar 和 NestedScrollView - Flutter RefreshIndicator doesn't work with TabBar and NestedScrollView 如何在 Flutter 中使用 FutureBuilder 显示来自 firestore 数据库的当前用户数据? - How to display the current user data from firestore database using FutureBuilder in Flutter? 当QuerySnapshot在Firestore中的addSnapshotListener中为null时 - When QuerySnapshot will be null in addSnapshotListener in Firestore Android / FireStore QuerySnapshot转换为CustomObject - Android/FireStore QuerySnapshot convert to CustomObject Flutter FutureBuilder 不继续/工作 - Flutter FutureBuilder not continuing / working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM