Skip to content

Commit a24db9c

Browse files
hoc081098collinjackson
authored andcommitted
[cloud_firestore] Fixes crash on Android when run a transaction (#87)
* Fix transaction crash on Android device * Update pub version * Bump for release * Reformat * Fix crash
1 parent 8ced962 commit a24db9c

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

packages/cloud_firestore/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.9+5
2+
3+
* Fixes a crash on Android when running a transaction without an internet connection.
4+
15
## 0.12.9+4
26

37
* Fix integer conversion warnings on iOS.

packages/cloud_firestore/android/src/main/java/io/flutter/plugins/firebase/cloudfirestore/CloudFirestorePlugin.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,10 @@ public void onMethodCall(MethodCall call, final Result result) {
373373
final Map<String, Object> arguments = call.arguments();
374374
getFirestore(arguments)
375375
.runTransaction(
376-
new Transaction.Function<Map<String, Object>>() {
376+
new Transaction.Function<TransactionResult>() {
377377
@Nullable
378378
@Override
379-
public Map<String, Object> apply(@NonNull Transaction transaction) {
379+
public TransactionResult apply(@NonNull Transaction transaction) {
380380
// Store transaction.
381381
int transactionId = (Integer) arguments.get("transactionId");
382382
transactions.append(transactionId, transaction);
@@ -424,23 +424,31 @@ public void notImplemented() {
424424
Tasks.await(transactionTCSTask, timeout, TimeUnit.MILLISECONDS);
425425

426426
// Once transaction completes return the result to the Dart side.
427-
return transactionResult;
427+
return new TransactionResult(transactionResult);
428428
} catch (Exception e) {
429429
Log.e(TAG, e.getMessage(), e);
430-
result.error("Error performing transaction", e.getMessage(), null);
430+
return new TransactionResult(e);
431431
}
432-
return null;
433432
}
434433
})
435434
.addOnCompleteListener(
436-
new OnCompleteListener<Map<String, Object>>() {
435+
new OnCompleteListener<TransactionResult>() {
437436
@Override
438-
public void onComplete(Task<Map<String, Object>> task) {
439-
if (task.isSuccessful()) {
440-
result.success(task.getResult());
441-
} else {
437+
public void onComplete(Task<TransactionResult> task) {
438+
if (!task.isSuccessful()) {
442439
result.error(
443440
"Error performing transaction", task.getException().getMessage(), null);
441+
return;
442+
}
443+
444+
TransactionResult transactionResult = task.getResult();
445+
if (transactionResult.exception == null) {
446+
result.success(transactionResult.result);
447+
} else {
448+
result.error(
449+
"Error performing transaction",
450+
transactionResult.exception.getMessage(),
451+
null);
444452
}
445453
}
446454
});
@@ -820,6 +828,21 @@ public void onFailure(@NonNull Exception e) {
820828
}
821829
}
822830
}
831+
832+
private static final class TransactionResult {
833+
final @Nullable Map<String, Object> result;
834+
final @Nullable Exception exception;
835+
836+
TransactionResult(@NonNull Exception exception) {
837+
this.exception = exception;
838+
this.result = null;
839+
}
840+
841+
TransactionResult(@Nullable Map<String, Object> result) {
842+
this.result = result;
843+
this.exception = null;
844+
}
845+
}
823846
}
824847

825848
final class FirestoreMessageCodec extends StandardMessageCodec {

0 commit comments

Comments
 (0)