我正在编写某种机器人(命令行应用程序),当我使用"forEach"方法时,我遇到异步执行问题.这是我正在尝试做的简化代码:
main() async {
print("main start");
await asyncOne();
print("main end");
}
asyncOne() async {
print("asyncOne start");
[1, 2, 3].forEach(await (num) async {
await asyncTwo(num);
});
print("asyncOne end");
}
asyncTwo(num) async
{
print("asyncTwo #${num}");
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
main start
asyncOne start
asyncOne end
main end
asyncTwo #1
asyncTwo #2
asyncTwo #3
Run Code Online (Sandbox Code Playgroud)
我想要得到的是:
main start
asyncOne start
asyncTwo #1
asyncTwo #2
asyncTwo #3
asyncOne end
main end
Run Code Online (Sandbox Code Playgroud)
如果有人知道我做错了什么,我会很感激.
我正在使用 Flutter 开发移动应用程序。我正在使用 flutter 块包https://pub.dev/packages/flutter_bloc来管理和设置块。但是当状态改变时,它不会更新小部件或视图。
我有一个名为 home_bloc.dart 的 bloc 类文件,具有以下实现。
class HomeEvent {
static const int FETCH_ARTICLES = 1;
static const int TOGGLE_IS_FILTERING = 2;
int _event = 0;
String _filterKeyword = "";
int get event => _event;
void set event(int event) {
this._event = event;
}
String get filterKeyword => _filterKeyword;
void set filterKeyword(String filterKeyword) {
this._filterKeyword = filterKeyword;
}
}
class HomeBloc extends Bloc<HomeEvent, HomeState> {
Repository _repository = Repository();
HomeState state = HomeState();
@override
HomeState …Run Code Online (Sandbox Code Playgroud)