![](/img/trans.png)
[英]The argument type 'Future<void> Function(FirebaseUser)' can't be assigned to the parameter type 'void Function(User)'
[英]The argument type 'Future<List<User>>' can't be assigned to the parameter type 'Future<void> Function()' in onRefresh when using RefreshIndicator
我有一個Future正在獲取和填充下面的用戶
Future<List<User>> _fetchUsersListUsingLoop() async {
try {
var response = await http.get(
Uri.parse(
"https://api.json-generator.com/templates/Eh5AlPjYVv6C/data"),
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer tltsp6dmnbif01jy9xfo9ssn4620u89xhuwcm5t3",
});
List<User> usersList = [];
for (var u in json.decode(response.body)) {
User user = User(u["email"], u["about"], u["name"], u["picture"],
u["index"], u["imageFetchType"]);
usersList.add(user);
}
return usersList;
} catch (e) {
log("FetchUsersListUsingLoopException $e");
rethrow;
}
}
下面是我如何在FutureBuilder中使用future
來調用_fetchUsersListUsingLoop()
然后在RefreshIndicator中使用ListView.builder在list
中顯示用戶
body: SizedBox(
child: FutureBuilder(
future: _fetchUsersListUsingLoop(),
builder: (BuildContext context, AsyncSnapshot asyncSnapshot) {
if (asyncSnapshot.data == null) {
return const Center(child: CircularProgressIndicator());
} else {
return RefreshIndicator(
// background color
backgroundColor: Colors.white,
// refresh circular progress indicator color
color: Colors.green,
onRefresh: _fetchUsersListUsingLoop(),
child: ListView.builder(
itemCount: asyncSnapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
contentPadding: const EdgeInsets.all(10),
leading: CircleAvatar(
backgroundImage:
NetworkImage(asyncSnapshot.data[index].picture),
),
title: Text(asyncSnapshot.data[index].name),
subtitle: Text(
"${asyncSnapshot.data[index].email} \nUsing NetworkImage with backgroundImage"),
);
},
));
}
}),
),
我得到The argument type 'Future<List<User>>' can't be assigned to the parameter type 'Future<void> Function()'.
此行onRefresh: _fetchUsersListUsingLoop(),
當我向下滑動刷新時,我如何能夠再次調用_fetchUsersListUsingLoop()
而不是調用_fetchUsersListUsingLoop
您應該使用setState
導致重建,以便FutureBuilder
再次獲取數據。
onRefresh: () => setState(() {})
話雖這么說,最好將你的未來存儲在initState
中,並在需要時通過調用setState
來更新這個未來。 這是來自文檔:
未來必須更早獲得,例如在 State.initState、State.didUpdateWidget 或 State.didChangeDependencies 期間。 在構造 FutureBuilder 時,不得在 State.build 或 StatelessWidget.build 方法調用期間創建它。 如果future與FutureBuilder同時創建,那么每次FutureBuilder的parent重建時,異步任務都會重新啟動。
@override
void initState() {
super.initState();
_usersFuture = _fetchUsersListUsingLoop();
}
然后:
onRefresh: () {
setState(() {
_usersFuture = _fetchUsersListUsingLoop();
});
}
當你這樣做時
onRefresh: _fetchUsersListUsingLoop(),
You are telling dart to call _fetchUsersListUsingLoop
and expect it to return a function, then to call said function, what you want to do instead is tell it to just call _fetchUsersListUsingLoop
:
onRefresh: _fetchUserListUsingLoop,
或“如果這不起作用:
onRefresh: () async => _fetchUserListUsingLoop(),
但問題是這實際上不起作用,為什么? 因為未來的建造者已經完成,沒有什么會改變它來解決你的問題; 您可以像這樣重新分配未來:
你需要聲明一個未來:
late Future<List<User>> _user;
child: FutureBuilder(
future: _users,
builder: (context, asyncSnapshot) {
...
onRefresh: _updateUsersFuture,
...
然后在你的 class 的某個地方你做:
Future<void> _updateUsersFuture() async {
final newUsers = await _fetchUserListUsingLoop();
setState(() => _users = newUsers);
}
並在初始化狀態:
@override
void initState() {
_users = _fetchUsersListUsingLoop();
super.initState();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.