[英]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;
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;
}
setState(() => this.postsList = data);
setState(() => this.postsList = data);
into the loadPostsList() function like the following: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.