44
55package io .flutter .plugins .firebase .database ;
66
7+ import android .app .Activity ;
78import android .util .Log ;
89import android .util .SparseArray ;
910import com .google .android .gms .tasks .Task ;
@@ -37,6 +38,7 @@ public class FirebaseDatabasePlugin implements MethodCallHandler {
3738 private static final String TAG = "FirebaseDatabasePlugin" ;
3839
3940 private final MethodChannel channel ;
41+ private final Activity activity ;
4042 private static final String EVENT_TYPE_CHILD_ADDED = "_EventType.childAdded" ;
4143 private static final String EVENT_TYPE_CHILD_REMOVED = "_EventType.childRemoved" ;
4244 private static final String EVENT_TYPE_CHILD_CHANGED = "_EventType.childChanged" ;
@@ -50,11 +52,12 @@ public class FirebaseDatabasePlugin implements MethodCallHandler {
5052 public static void registerWith (PluginRegistry .Registrar registrar ) {
5153 final MethodChannel channel =
5254 new MethodChannel (registrar .messenger (), "plugins.flutter.io/firebase_database" );
53- channel .setMethodCallHandler (new FirebaseDatabasePlugin (channel ));
55+ channel .setMethodCallHandler (new FirebaseDatabasePlugin (channel , registrar . activity () ));
5456 }
5557
56- private FirebaseDatabasePlugin (MethodChannel channel ) {
58+ private FirebaseDatabasePlugin (MethodChannel channel , Activity activity ) {
5759 this .channel = channel ;
60+ this .activity = activity ;
5861 }
5962
6063 private DatabaseReference getReference (FirebaseDatabase database , Map <String , Object > arguments ) {
@@ -333,7 +336,7 @@ public Transaction.Result doTransaction(MutableData mutableData) {
333336 final Task <Map <String , Object >> updateMutableDataTCSTask =
334337 updateMutableDataTCS .getTask ();
335338
336- Map <String , Object > doTransactionMap = new HashMap <>();
339+ final Map <String , Object > doTransactionMap = new HashMap <>();
337340 doTransactionMap .put ("transactionKey" , arguments .get ("transactionKey" ));
338341
339342 final Map <String , Object > snapshotMap = new HashMap <>();
@@ -342,39 +345,46 @@ public Transaction.Result doTransaction(MutableData mutableData) {
342345 doTransactionMap .put ("snapshot" , snapshotMap );
343346
344347 // Return snapshot to Dart side for update.
345- channel .invokeMethod (
346- "DoTransaction" ,
347- doTransactionMap ,
348- new MethodChannel .Result () {
348+ activity .runOnUiThread (
349+ new Runnable () {
349350 @ Override
350- @ SuppressWarnings ("unchecked" )
351- public void success (Object result ) {
352- updateMutableDataTCS .setResult ((Map <String , Object >) result );
353- }
354-
355- @ Override
356- public void error (
357- String errorCode , String errorMessage , Object errorDetails ) {
358- String exceptionMessage =
359- "Error code: "
360- + errorCode
361- + "\n Error message: "
362- + errorMessage
363- + "\n Error details: "
364- + errorDetails ;
365- updateMutableDataTCS .setException (new Exception (exceptionMessage ));
366- }
367-
368- @ Override
369- public void notImplemented () {
370- updateMutableDataTCS .setException (
371- new Exception ("DoTransaction not implemented on Dart side." ));
351+ public void run () {
352+ channel .invokeMethod (
353+ "DoTransaction" ,
354+ doTransactionMap ,
355+ new MethodChannel .Result () {
356+ @ Override
357+ @ SuppressWarnings ("unchecked" )
358+ public void success (Object result ) {
359+ updateMutableDataTCS .setResult ((Map <String , Object >) result );
360+ }
361+
362+ @ Override
363+ public void error (
364+ String errorCode , String errorMessage , Object errorDetails ) {
365+ String exceptionMessage =
366+ "Error code: "
367+ + errorCode
368+ + "\n Error message: "
369+ + errorMessage
370+ + "\n Error details: "
371+ + errorDetails ;
372+ updateMutableDataTCS .setException (
373+ new Exception (exceptionMessage ));
374+ }
375+
376+ @ Override
377+ public void notImplemented () {
378+ updateMutableDataTCS .setException (
379+ new Exception ("DoTransaction not implemented on Dart side." ));
380+ }
381+ });
372382 }
373383 });
374384
375385 try {
376386 // Wait for updated snapshot from the Dart side.
377- Map <String , Object > updatedSnapshotMap =
387+ final Map <String , Object > updatedSnapshotMap =
378388 Tasks .await (
379389 updateMutableDataTCSTask ,
380390 (int ) arguments .get ("transactionTimeout" ),
@@ -394,7 +404,7 @@ public void notImplemented() {
394404 @ Override
395405 public void onComplete (
396406 DatabaseError databaseError , boolean committed , DataSnapshot dataSnapshot ) {
397- Map <String , Object > completionMap = new HashMap <>();
407+ final Map <String , Object > completionMap = new HashMap <>();
398408 completionMap .put ("transactionKey" , arguments .get ("transactionKey" ));
399409 if (databaseError != null ) {
400410 completionMap .put ("error" , asMap (databaseError ));
@@ -408,7 +418,12 @@ public void onComplete(
408418 }
409419
410420 // Invoke transaction completion on the Dart side.
411- result .success (completionMap );
421+ activity .runOnUiThread (
422+ new Runnable () {
423+ public void run () {
424+ result .success (completionMap );
425+ }
426+ });
412427 }
413428 });
414429 break ;
0 commit comments