Skip to content

Commit

Permalink
fix(firestore): fix an issue that would cause FieldValue.increment to…
Browse files Browse the repository at this point in the history
… not work for big int (#12426)
  • Loading branch information
Lyokone committed Mar 5, 2024
1 parent 741c551 commit a776dec
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Expand Up @@ -31,6 +31,15 @@ void runFieldValueTests() {
expect(snapshot.data()!['foo'], equals(3));
});

testWidgets('increments a big number if it exists', (_) async {
DocumentReference<Map<String, dynamic>> doc =
await initializeTest('field-value-increment-exists');
await doc.set({'foo': 0});
await doc.update({'foo': FieldValue.increment(2148000000)});
DocumentSnapshot<Map<String, dynamic>> snapshot = await doc.get();
expect(snapshot.data()!['foo'], equals(2148000000));
});

testWidgets('decrements a number', (_) async {
DocumentReference<Map<String, dynamic>> doc =
await initializeTest('field-value-decrement-exists');
Expand Down
Expand Up @@ -82,8 +82,14 @@ class FirestoreMessageCodec extends StandardMessageCodec {
} else if (value is FieldValuePlatform) {
MethodChannelFieldValue delegate = FieldValuePlatform.getDelegate(value);
final int code = _kFieldValueCodes[delegate.type]!;
buffer.putUint8(code);
if (delegate.value != null) writeValue(buffer, delegate.value);
// We turn int into double here to avoid precision loss.
if (delegate.type == FieldValueType.incrementInteger) {
buffer.putUint8(_kIncrementDouble);
writeValue(buffer, (delegate.value as int).toDouble());
} else {
buffer.putUint8(code);
if (delegate.value != null) writeValue(buffer, delegate.value);
}
} else if (value is FieldPathType) {
final int code = _kFieldPathCodes[value]!;
buffer.putUint8(code);
Expand Down

0 comments on commit a776dec

Please sign in to comment.