Flutter 异步代码中错误的执行顺序
Wrong order of execution in Flutter asynchronous code
我在 Flutter 应用程序中遇到期货问题。
void saveCats() async {
var cats = await convertToCats(_rawData);
await DatabaseProvider.db.addCats(cats);
}
Future<List<Cat>> convertToCats(CatList catData) async {
var cats = <Cat>[];
await catData.forEach(key, value) async {
var pos = await calculatePos();
print('This should come first');
cats.add(Cat(name: key, pos: pos);
}
}
Future<int> calculatePos() async {
return await DatabaseProvider.db.calculatePos();
}
database.dart:
Future<void> addCats(List<Cat> cats) async {
print('This should come second');
// Do adding stuff here
}
Future<int> calculatePos() async {
// Some code to calculate the position
return pos;
}
在上面的代码中,saveCats
函数在点击按钮时被调用。此函数将一些原始数据转换为 Cat 模型列表,并将它们添加到数据库中。作为此转换过程的一部分,它会计算列表中猫的 pos
。我的问题是,我希望在我的两个 print
语句中,forEach
循环中的语句应该在 addCats
数据库函数中的语句之前。但相反,它们以相反的顺序出现。我哪里错了?
您不能在 List.forEach() 或 Map.forEach() 中 async/await
,因为它们 return 都是无效的。
要么使用
await Future.forEach([1, 2, 3], (num) async {
await asyncMethod(num);
});
或类似的东西;
https://api.flutter.dev/flutter/dart-async/Future/forEach.html
forEach
通常不会按照您的预期执行,因为提供的函数作为闭包运行。
当您想遍历列表对每个元素执行某些操作以使用 for
(或更实用的类型方法之一,如 map
)时,这会更自然。
不清楚 CatList
是什么类型,所以这是近似值,但您需要更像:
Future<List<Cat>> convertToCats(CatList catData) async {
var cats = <Cat>[];
for (var i = 0; i < catData.length; i++) {
var key = catData[i].key;
var pos = await calculatePos();
print('This should come first');
cats.add(Cat(name: key, pos: pos));
}
return cats;
}
或
Future<List<Cat>> convertToCats(CatList catData) async {
return catData.map(...some mapping function...).toList();
}
我在 Flutter 应用程序中遇到期货问题。
void saveCats() async {
var cats = await convertToCats(_rawData);
await DatabaseProvider.db.addCats(cats);
}
Future<List<Cat>> convertToCats(CatList catData) async {
var cats = <Cat>[];
await catData.forEach(key, value) async {
var pos = await calculatePos();
print('This should come first');
cats.add(Cat(name: key, pos: pos);
}
}
Future<int> calculatePos() async {
return await DatabaseProvider.db.calculatePos();
}
database.dart:
Future<void> addCats(List<Cat> cats) async {
print('This should come second');
// Do adding stuff here
}
Future<int> calculatePos() async {
// Some code to calculate the position
return pos;
}
在上面的代码中,saveCats
函数在点击按钮时被调用。此函数将一些原始数据转换为 Cat 模型列表,并将它们添加到数据库中。作为此转换过程的一部分,它会计算列表中猫的 pos
。我的问题是,我希望在我的两个 print
语句中,forEach
循环中的语句应该在 addCats
数据库函数中的语句之前。但相反,它们以相反的顺序出现。我哪里错了?
您不能在 List.forEach() 或 Map.forEach() 中 async/await
,因为它们 return 都是无效的。
要么使用
await Future.forEach([1, 2, 3], (num) async {
await asyncMethod(num);
});
或类似的东西; https://api.flutter.dev/flutter/dart-async/Future/forEach.html
forEach
通常不会按照您的预期执行,因为提供的函数作为闭包运行。
当您想遍历列表对每个元素执行某些操作以使用 for
(或更实用的类型方法之一,如 map
)时,这会更自然。
不清楚 CatList
是什么类型,所以这是近似值,但您需要更像:
Future<List<Cat>> convertToCats(CatList catData) async {
var cats = <Cat>[];
for (var i = 0; i < catData.length; i++) {
var key = catData[i].key;
var pos = await calculatePos();
print('This should come first');
cats.add(Cat(name: key, pos: pos));
}
return cats;
}
或
Future<List<Cat>> convertToCats(CatList catData) async {
return catData.map(...some mapping function...).toList();
}