🐛 [cloud_firestore] Error with Firestore Increment Value for Large Integers
Bug report
Describe the bug I'm experiencing an issue with Firestore's FieldValue.increment() function when using large integer values. The increment works correctly for smaller numbers, but it produces incorrect results, even negative values, for numbers above 2147483648. I asked the question on stackoverflow and @DougStevenson suggests I include it as a possible bug within the FlutterFire SDK
Steps to reproduce
Steps to reproduce the behavior:
- Build a Firestore Database with
testingcollection andincrementdocument - Include the
testfield within theincrementdocument with the initial numeric value of 0 - Using cloud_firestore package on flutter, implement the following code
Future<bool> incrementTest() async {
WriteBatch batch = db.batch();
int incrementValue = 2147483648;
DocumentReference doc = db
.collection('testing')
.doc('increment');
batch.update(doc, {
'test': FieldValue.increment(incrementValue),
});
log('Test incrementValue: $incrementValue');
try {
await batch.commit();
return true;
} catch (e) {
log('$e');
return false;
}
}
- See error or incorrect behavior inside the database
Expected behavior
Three examples:
Example 1 (Ok):
Initial test value in Firestore = 0 Increment Value = 1000 Expected Result = 1000 Final test value in Firestore = 1000 (Ok)
Example 2 (Calculation error):
Initial test value in Firestore = 0 Increment Value = 2148000000 Expected Result = 2148000000 Final test value in Firestore = -2146967296 (Error)
Example 3 (Calculation error):
Initial test value in Firestore = 0 Increment Value = 10000000000 Expected Result = 10000000000 Final test value in Firestore = 1410065408 (Error)
Additional context
-
The issue persists with both WriteBatch and direct doc.update() operations.
-
Firebase documentation states a limit of -2^63 to 2^63-1 for integer operations, which is much higher than the problematic value.
-
The exact value where the error begins to appear is 2147483648 which is equivalent to 2^32 / 2 (32-bit signed integer)
Flutter doctor
Run flutter doctor and paste the output below:
Click To Expand
✓] Flutter (Channel stable, 3.7.12, on macOS 14.2.1 23C71 darwin-x64, locale es-419)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.86.1)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
Flutter dependencies
Run flutter pub deps -- --style=compact and paste the output below:
Click To Expand
Dart SDK 2.19.6
Flutter SDK 3.7.12
apliansprocessing 0.1.0
dependencies:
- cached_network_image 3.2.3 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web]
- cloud_firestore 4.14.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- datetime_picker_formfield 2.0.1 [flutter intl]
- device_info_plus 9.1.1 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32 win32_registry]
- firebase_auth 4.16.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta]
- firebase_core 2.24.2 [firebase_core_platform_interface firebase_core_web flutter meta]
- firebase_messaging 14.7.10 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta]
- firebase_storage 11.6.0 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter]
- fl_chart 0.62.0 [equatable flutter]
- flutter 0.0.0 [characters collection js material_color_utilities meta vector_math sky_engine]
- flutter_cache_manager 3.3.1 [clock collection file flutter http path path_provider rxdart sqflite uuid]
- flutter_localizations 0.0.0 [flutter intl characters clock collection js material_color_utilities meta path vector_math]
- flutter_slidable 3.0.1 [flutter]
- font_awesome_flutter 10.4.0 [flutter]
- intl 0.17.0 [clock path]
- rxdart 0.27.7
- shared_preferences 2.2.2 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shared_preferences_web shared_preferences_windows]
dev dependencies:
- flutter_lints 2.0.3 [lints]
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters collection js matcher material_color_utilities meta source_span stream_channel string_scanner term_glyph]
transitive dependencies:
- _flutterfire_internals 1.3.16 [collection firebase_core firebase_core_platform_interface flutter meta]
- async 2.10.0 [collection meta]
- boolean_selector 2.1.1 [source_span string_scanner]
- cached_network_image_platform_interface 2.0.0 [flutter flutter_cache_manager]
- cached_network_image_web 1.0.2 [flutter flutter_cache_manager cached_network_image_platform_interface]
- characters 1.2.1
- clock 1.1.1
- cloud_firestore_platform_interface 6.1.0 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 3.9.0 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- collection 1.17.0
- crypto 3.0.3 [typed_data]
- device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface]
- equatable 2.0.5 [collection meta]
- fake_async 1.3.1 [clock collection]
- ffi 2.0.2
- file 6.1.4 [meta path]
- firebase_auth_platform_interface 7.0.9 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_auth_web 5.8.13 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser js meta]
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.10.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta]
- firebase_messaging_platform_interface 4.5.18 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface]
- firebase_messaging_web 3.5.18 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta]
- firebase_storage_platform_interface 5.1.3 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- firebase_storage_web 3.6.17 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta]
- flutter_blurhash 0.7.0 [flutter]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- http 0.13.6 [async http_parser meta]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- js 0.6.5 [meta]
- lints 2.0.1
- matcher 0.12.13 [meta stack_trace]
- material_color_utilities 0.2.0
- meta 1.8.0
- octo_image 1.0.2 [flutter flutter_blurhash]
- path 1.8.2
- path_provider 2.1.1 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows]
- path_provider_android 2.2.1 [flutter path_provider_platform_interface]
- path_provider_foundation 2.3.1 [flutter path_provider_platform_interface]
- path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories]
- path_provider_platform_interface 2.1.1 [flutter platform plugin_platform_interface]
- path_provider_windows 2.2.1 [ffi flutter path path_provider_platform_interface win32]
- platform 3.1.3
- plugin_platform_interface 2.1.6 [meta]
- shared_preferences_android 2.2.1 [flutter shared_preferences_platform_interface]
- shared_preferences_foundation 2.3.4 [flutter shared_preferences_platform_interface]
- shared_preferences_linux 2.3.2 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface]
- shared_preferences_platform_interface 2.3.1 [flutter plugin_platform_interface]
- shared_preferences_web 2.2.1 [flutter flutter_web_plugins shared_preferences_platform_interface]
- shared_preferences_windows 2.3.2 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface]
- sky_engine 0.0.99
- source_span 1.9.1 [collection path term_glyph]
- sprintf 7.0.0
- sqflite 2.2.8+4 [flutter sqflite_common path]
- sqflite_common 2.4.5+1 [synchronized path meta]
- stack_trace 1.11.0 [path]
- stream_channel 2.1.1 [async]
- string_scanner 1.2.0 [source_span]
- synchronized 3.1.0
- term_glyph 1.2.1
- test_api 0.4.16 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.2 [collection]
- uuid 4.1.0 [crypto sprintf]
- vector_math 2.1.4
- win32 4.1.4 [ffi]
- win32_registry 1.1.0 [ffi win32]
- xdg_directories 1.0.3 [meta path]
Thanks for the report @dlondonog Using the details provided, I was able to replicate this running on mobile. Looks like more of a dart limitation instead of the plugin, but I'll label this for team's input / attention.
If you turn your int into a double, by adding a .0 at the end for instance, it will get interpreted as a double and it will work properly. I'll see if we can interpret it as double everytime it should not add a huge overhead to native.