Skip to content

Commit 49476dc

Browse files
committed
fix: improve error handling and formatting in FirebaseDatabasePlugin
1 parent c00af59 commit 49476dc

File tree

1 file changed

+15
-12
lines changed
  • packages/firebase_database/firebase_database/android/src/main/kotlin/io/flutter/plugins/firebase/database

1 file changed

+15
-12
lines changed

packages/firebase_database/firebase_database/android/src/main/kotlin/io/flutter/plugins/firebase/database/FirebaseDatabasePlugin.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class FirebaseDatabasePlugin :
238238
@Suppress("UNCHECKED_CAST")
239239
val value = arguments[Constants.VALUE] as Map<String, Any>
240240
Tasks.await(ref.updateChildren(value))
241-
taskCompletionSource.setResult(null)
241+
taskCompletionSource.setResult(null)
242242
} catch (e: Exception) {
243243
taskCompletionSource.setException(e)
244244
}
@@ -605,7 +605,7 @@ class FirebaseDatabasePlugin :
605605
try {
606606
val database = getDatabaseFromPigeonApp(app)
607607
val reference = database.getReference(request.path)
608-
608+
609609
// Handle priority type conversion - Firebase Database expects Any? but Pigeon sends Object?
610610
val priority = when (request.priority) {
611611
is String -> request.priority
@@ -640,14 +640,17 @@ class FirebaseDatabasePlugin :
640640
}
641641

642642
override fun databaseReferenceUpdate(app: DatabasePigeonFirebaseApp, request: UpdateRequest, callback: (KotlinResult<Unit>) -> Unit) {
643-
try {
644643
val database = getDatabaseFromPigeonApp(app)
645644
val reference = database.getReference(request.path)
646-
reference.updateChildren(request.value)
647-
callback(KotlinResult.success(Unit))
648-
} catch (e: Exception) {
649-
callback(KotlinResult.failure(e))
650-
}
645+
reference.updateChildren(request.value).addOnCompleteListener { task->
646+
if(task.isSuccessful){
647+
callback(KotlinResult.success(Unit))
648+
}
649+
else {
650+
val exception = task.exception
651+
callback(KotlinResult.failure(FlutterError("firebase_database", exception?.message, null)))
652+
}
653+
}
651654
}
652655

653656
override fun databaseReferenceSetPriority(app: DatabasePigeonFirebaseApp, request: DatabaseReferenceRequest, callback: (KotlinResult<Unit>) -> Unit) {
@@ -740,8 +743,8 @@ class FirebaseDatabasePlugin :
740743
mutableData.value = result.value
741744
return com.google.firebase.database.Transaction.success(mutableData)
742745
}
743-
744-
override fun onComplete(error: com.google.firebase.database.DatabaseError?, committed: Boolean, currentData: com.google.firebase.database.DataSnapshot?) {
746+
747+
override fun onComplete(error: com.google.firebase.database.DatabaseError?, committed: Boolean, currentData: com.google.firebase.database.DataSnapshot?) {
745748
// Store the transaction result for later retrieval
746749
val result = mapOf(
747750
"committed" to committed,
@@ -752,7 +755,7 @@ class FirebaseDatabasePlugin :
752755
)
753756
)
754757
transactionResults[request.transactionKey] = result
755-
758+
756759
// Complete the transaction - simplified like iOS
757760
if (error != null) {
758761
val ex = FlutterFirebaseDatabaseException.fromDatabaseError(error)
@@ -961,7 +964,7 @@ class FirebaseDatabasePlugin :
961964
})
962965
eventChannel.setStreamHandler(streamHandler)
963966
streamHandlers[eventChannel] = streamHandler
964-
967+
965968
callback(KotlinResult.success(channelName))
966969
} catch (e: Exception) {
967970
callback(KotlinResult.failure(e))

0 commit comments

Comments
 (0)