我正在尝试使用Flutter在Firebase数据库中运行事务,但出现错误,有人可以帮我解决这个问题吗?
代码:
ref.runTransaction((MutableData transaction) async {
transaction.value = (transaction.value ?? 0) + 1;
return transaction;
});
错误:
E/RepoOperation(16416): Caught Throwable.
E/RepoOperation(16416): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: FirebaseDatabaseWorker
E/RepoOperation(16416): at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:605)
E/RepoOperation(16416): at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:515)
E/RepoOperation(16416): at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:76)
E/RepoOperation(16416): at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:166)
E/RepoOperation(16416): at io.flutter.view.FlutterNativeView.send(FlutterNativeView.java:155)
E/RepoOperation(16416): at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:98)
E/RepoOperation(16416): at io.flutter.plugins.firebase.database.FirebaseDatabasePlugin$1.doTransaction(FirebaseDatabasePlugin.java:345)
E/RepoOperation(16416): at com.google.firebase.database.core.Repo.startTransaction(com.google.firebase:firebase-database@@17.0.0:831)
E/RepoOperation(16416): at com.google.firebase.database.DatabaseReference$4.run(com.google.firebase:firebase-database@@17.0.0:511)
E/RepoOperation(16416): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
E/RepoOperation(16416): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/RepoOperation(16416): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
E/RepoOperation(16416): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/RepoOperation(16416): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/RepoOperation(16416): at java.lang.Thread.run(Thread.java:764)
谢谢。
答案 0 :(得分:3)
使用颤动dev channel
时遇到相同的错误。
转到stable channel
为我修复了该问题。要检查您所在的频道,请flutter channel
要切换到稳定频道,请执行以下操作:
flutter channel stable
flutter upgrade
答案 1 :(得分:0)