diff --git a/packages/firebase_database/CHANGELOG.md b/packages/firebase_database/CHANGELOG.md index 3f8772587402..0e032e4dc2d3 100644 --- a/packages/firebase_database/CHANGELOG.md +++ b/packages/firebase_database/CHANGELOG.md @@ -1,6 +1,8 @@ ## 3.0.4 -* Fix transactions on +* Updated transactions implementation on Android for compatibility with + newer versions of Flutter engine that require channel calls be made + on the UI thread. ## 3.0.3 diff --git a/packages/firebase_database/android/src/main/java/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.java b/packages/firebase_database/android/src/main/java/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.java index 2f1f7145b9d9..c3fb751e5070 100644 --- a/packages/firebase_database/android/src/main/java/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.java +++ b/packages/firebase_database/android/src/main/java/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.java @@ -4,6 +4,7 @@ package io.flutter.plugins.firebase.database; +import android.app.Activity; import android.util.Log; import android.util.SparseArray; import com.google.android.gms.tasks.Task; @@ -37,6 +38,7 @@ public class FirebaseDatabasePlugin implements MethodCallHandler { private static final String TAG = "FirebaseDatabasePlugin"; private final MethodChannel channel; + private final Activity activity; private static final String EVENT_TYPE_CHILD_ADDED = "_EventType.childAdded"; private static final String EVENT_TYPE_CHILD_REMOVED = "_EventType.childRemoved"; private static final String EVENT_TYPE_CHILD_CHANGED = "_EventType.childChanged"; @@ -50,11 +52,12 @@ public class FirebaseDatabasePlugin implements MethodCallHandler { public static void registerWith(PluginRegistry.Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/firebase_database"); - channel.setMethodCallHandler(new FirebaseDatabasePlugin(channel)); + channel.setMethodCallHandler(new FirebaseDatabasePlugin(channel, registrar.activity())); } - private FirebaseDatabasePlugin(MethodChannel channel) { + private FirebaseDatabasePlugin(MethodChannel channel, Activity activity) { this.channel = channel; + this.activity = activity; } private DatabaseReference getReference(FirebaseDatabase database, Map arguments) { @@ -333,7 +336,7 @@ public Transaction.Result doTransaction(MutableData mutableData) { final Task> updateMutableDataTCSTask = updateMutableDataTCS.getTask(); - Map doTransactionMap = new HashMap<>(); + final Map doTransactionMap = new HashMap<>(); doTransactionMap.put("transactionKey", arguments.get("transactionKey")); final Map snapshotMap = new HashMap<>(); @@ -342,39 +345,46 @@ public Transaction.Result doTransaction(MutableData mutableData) { doTransactionMap.put("snapshot", snapshotMap); // Return snapshot to Dart side for update. - channel.invokeMethod( - "DoTransaction", - doTransactionMap, - new MethodChannel.Result() { + activity.runOnUiThread( + new Runnable() { @Override - @SuppressWarnings("unchecked") - public void success(Object result) { - updateMutableDataTCS.setResult((Map) result); - } - - @Override - public void error( - String errorCode, String errorMessage, Object errorDetails) { - String exceptionMessage = - "Error code: " - + errorCode - + "\nError message: " - + errorMessage - + "\nError details: " - + errorDetails; - updateMutableDataTCS.setException(new Exception(exceptionMessage)); - } - - @Override - public void notImplemented() { - updateMutableDataTCS.setException( - new Exception("DoTransaction not implemented on Dart side.")); + public void run() { + channel.invokeMethod( + "DoTransaction", + doTransactionMap, + new MethodChannel.Result() { + @Override + @SuppressWarnings("unchecked") + public void success(Object result) { + updateMutableDataTCS.setResult((Map) result); + } + + @Override + public void error( + String errorCode, String errorMessage, Object errorDetails) { + String exceptionMessage = + "Error code: " + + errorCode + + "\nError message: " + + errorMessage + + "\nError details: " + + errorDetails; + updateMutableDataTCS.setException( + new Exception(exceptionMessage)); + } + + @Override + public void notImplemented() { + updateMutableDataTCS.setException( + new Exception("DoTransaction not implemented on Dart side.")); + } + }); } }); try { // Wait for updated snapshot from the Dart side. - Map updatedSnapshotMap = + final Map updatedSnapshotMap = Tasks.await( updateMutableDataTCSTask, (int) arguments.get("transactionTimeout"), @@ -394,7 +404,7 @@ public void notImplemented() { @Override public void onComplete( DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) { - Map completionMap = new HashMap<>(); + final Map completionMap = new HashMap<>(); completionMap.put("transactionKey", arguments.get("transactionKey")); if (databaseError != null) { completionMap.put("error", asMap(databaseError)); @@ -408,7 +418,12 @@ public void onComplete( } // Invoke transaction completion on the Dart side. - result.success(completionMap); + activity.runOnUiThread( + new Runnable() { + public void run() { + result.success(completionMap); + } + }); } }); break; diff --git a/packages/firebase_database/pubspec.yaml b/packages/firebase_database/pubspec.yaml index 59f8aa06ce4f..d91420632811 100755 --- a/packages/firebase_database/pubspec.yaml +++ b/packages/firebase_database/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Database, a cloud-hosted NoSQL database with realtime data syncing across Android and iOS clients, and offline access. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_database -version: 3.0.3 +version: 3.0.4 flutter: plugin: