在 Flutter 中等待异步
Waiting async in Flutter
我正在尝试使用 Flutter 编写 Matrix 服务器客户端应用程序。
我从用户所属的服务器请求群组。
Future<List> joinedRooms(String accessToken) async {
String url = server + "/_matrix/client/r0/joined_rooms";
Map<String, String> headers = {"Authorization": "Bearer $accessToken"};
Response response = await get(url, headers: headers);
JoinedRooms roomsID = JoinedRooms.fromJson(jsonDecode(response.body));
List names = [];
roomsID.joinedRooms.forEach((roomID) async {
await roomState(accessToken, roomID).then((value) async {
await names.add(value[6].content.name);
});
return names;
});
以下值即将用于 response.body
{
"joined_rooms": [
"!JSHWMPAgoJdkQvRQrr:example.com",
"!WzjRnPpUASluIsGTuo: example.com"
]
}
使用.forEach
方法,我想为每个组id请求组名,并将其添加到names
列表中。
Future<List<RoomState>> roomState(String accessToken, String roomID) async {
String url = server + "/_matrix/client/r0/rooms/$roomID/state";
Map<String, String> headers = {"Authorization": "Bearer $accessToken"};
Response response = await get(url, headers: headers);
List<RoomState> states = (json.decode(response.body) as List).map((i) => RoomState.fromJson(i)).toList();
return states;
}
names
的列表总是空的。 async
没有在等待代码片段。我尝试了很多方法都没有成功。
你不是在等待 forEach
中 运行 的函数
roomsID.joinedRooms.forEach((roomID) async {
await ...
});
await 不起作用,因为 forEach 内的函数没有说明要等待。
在 DartPad
上尝试下面的示例
import 'dart:async';
void main() async {
List<int> list = [1, 2, 3];
list.forEach((value) async {
await Future.delayed(Duration(seconds: value));
print('Waited $value');
});
print('Finished main');
}
结果是:
Finished main
Waited 1
Waited 2
Waited 3
因为forEach不等待里面的函数。
试试看:
for(.. in ..) {
await ...
}
我正在尝试使用 Flutter 编写 Matrix 服务器客户端应用程序。
我从用户所属的服务器请求群组。
Future<List> joinedRooms(String accessToken) async {
String url = server + "/_matrix/client/r0/joined_rooms";
Map<String, String> headers = {"Authorization": "Bearer $accessToken"};
Response response = await get(url, headers: headers);
JoinedRooms roomsID = JoinedRooms.fromJson(jsonDecode(response.body));
List names = [];
roomsID.joinedRooms.forEach((roomID) async {
await roomState(accessToken, roomID).then((value) async {
await names.add(value[6].content.name);
});
return names;
});
以下值即将用于 response.body
{
"joined_rooms": [
"!JSHWMPAgoJdkQvRQrr:example.com",
"!WzjRnPpUASluIsGTuo: example.com"
]
}
使用.forEach
方法,我想为每个组id请求组名,并将其添加到names
列表中。
Future<List<RoomState>> roomState(String accessToken, String roomID) async {
String url = server + "/_matrix/client/r0/rooms/$roomID/state";
Map<String, String> headers = {"Authorization": "Bearer $accessToken"};
Response response = await get(url, headers: headers);
List<RoomState> states = (json.decode(response.body) as List).map((i) => RoomState.fromJson(i)).toList();
return states;
}
names
的列表总是空的。 async
没有在等待代码片段。我尝试了很多方法都没有成功。
你不是在等待 forEach
中 运行 的函数roomsID.joinedRooms.forEach((roomID) async {
await ...
});
await 不起作用,因为 forEach 内的函数没有说明要等待。 在 DartPad
上尝试下面的示例import 'dart:async';
void main() async {
List<int> list = [1, 2, 3];
list.forEach((value) async {
await Future.delayed(Duration(seconds: value));
print('Waited $value');
});
print('Finished main');
}
结果是:
Finished main
Waited 1
Waited 2
Waited 3
因为forEach不等待里面的函数。
试试看:
for(.. in ..) {
await ...
}