首页 > 解决方案 > 异步任务在所有未来完成之前结束

问题描述

我正在使用 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)

谢谢

标签: androiddartflutter

解决方案


推荐阅读