android - 异步任务在所有未来完成之前结束
问题描述
我正在使用 Flutter 编写一个应用程序,在这个应用程序中,我需要在按钮回调 ( onPressed()
) 中调用一个异步函数。问题是,当我单击按钮时,会抛出一个异常,指出函数无法完成,因为执行它的任务已经完成(在函数完成之前)。我试图将 onPressed 函数标记为异步,甚至等待其他异步函数结果,但我仍然在运行时抛出异常。
我该怎么做才能使按钮回调在退出之前等待异步函数完成?或者我该如何解决这个问题?
这是导致问题的代码:
SimpleDialogOption(child: Text('Guardar'), onPressed: () async {
var item = controller.text;
String dbparent;
switch (selectedFGroup) {
case FGroups.Azucar: dbparent = 'azucar'; break;
case FGroups.Cereales: dbparent = 'cereales'; break;
case FGroups.Frutas: dbparent = 'frutas'; break;
case FGroups.Grasa: dbparent = 'grasa'; break;
case FGroups.Leche: dbparent = 'leche'; break;
case FGroups.Leguminosa: dbparent = 'leguminosa'; break;
case FGroups.Poa: dbparent = 'poa'; break;
case FGroups.Verduras: dbparent = 'verduras'; break;
default: print('${selectedFGroup.toString()} not found, returning'); return;
}
// Update it in the local db
User.user.firebaseUser[dbparent][item] = false;
User.user.update(User.user.firebaseUser, 'firebaseUser');
// Update it in the Firebase backend
await Firestore.instance.runTransaction((transaction) async {
CollectionReference reference = Firestore.instance.collection('users');
DocumentSnapshot snapshot = await transaction.get(reference.document('-LEzXx9yRMBNxOyAhHyO'));
await transaction.update(snapshot.reference, {'firebaseUser': User.user.firebaseUser});
});
print('addFoodItem: DONE!');
},),
这是日志(我无法将其格式化为代码块):
I/FlutterActivityDelegate(9746):onResume 将当前活动设置为此
E/BpSurfaceComposerClient(9746):交易失败(-1)
E/BpSurfaceComposerClient(9746):交易失败(-1)
I/flutter(9746):FGroups。 Verduras
I/flutter (9746):addFoodItem:完成!
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): 处理方法调用结果失败
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): java.lang.IllegalStateException: 任务已经完成
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746):在 com.google.android.gms.common.internal.Preconditions.checkState(未知来源:8) E/MethodChannel#plugins.flutter.io/cloud_firestore(9746 ):在 com.google.android.gms.tasks.zzu.zzdr(未知来源:8) E/MethodChannel#plugins.flutter.io/cloud_firestore(9746):在 com.google.android.gms.tasks.zzu。 setResult(Unknown Source:3)
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): at com.google.android.gms.tasks.TaskCompletionSource.setResult(Unknown Source:2)
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$3$1.success(CloudFirestorePlugin.java:283) E/MethodChannel#plugins.flutter.io/cloud_firestore( 9746):在 io.flutter.plugin.common.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:169)E/MethodChannel#plugins.flutter.io/cloud_firestore(9746):在 io.flutter.view.FlutterNativeView.handlePlatformMessageResponse( FlutterNativeView.java:171) E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): 在 android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): 在 android .os.MessageQueue.next(MessageQueue.java:325)
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746):在 android.os.Looper.loop(Looper.java:142)
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746):在 android.app。 ActivityThread.main(ActivityThread.java:6798)
E/MethodChannel#plugins.flutter.io/cloud_firestore(9746): 在 java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/cloud_firestore (9746):在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)E/MethodChannel#plugins.flutter.io/cloud_firestore(9746):在 com.android.internal.os.ZygoteInit .main(ZygoteInit.java:767)
谢谢
解决方案
推荐阅读
- sass - 如何在包含之前向 Sass/SCSS 文件提供配置变量?
- html - 图像超出 Bootstrap 4 中的列宽
- react-native - 如何重置导航参数
- reactjs - 如何在 React Native 中创建和导航动态路由或屏幕
- scala - 在scala中使用委托令牌对kafka进行身份验证spark结构化流式传输
- node.js - 如何使用 Sequelize 计算列的所有值的总和
- python - 用于图像过滤器的 Python - TypeError:“NoneType”对象不可下标
- javascript - 如何将状态值作为道具传递给子组件
- c# - C# 使用起始位置和循环排序对象列表
- swift - 如何在使用 MVVM 架构时为已发布的属性包装器创建可选参数?