我正在使用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(未知来源:3)
E / MethodChannel#plugins.flutter.io / cloud_firestore(9746):在 com.google.android.gms.tasks.TaskCompletionSource.setResult(未知 来源:2)
E / MethodChannel#plugins.flutter.io / cloud_firestore(9746):在 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(本机方法)
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(本机方法)
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)
谢谢