flutterfire
flutterfire copied to clipboard
🐛 [cloud_firestore] [web] Modifying settings results in uncaught exception on hot reload/restart
Bug report
Setting FirebaseFirestore.instance.settings
causes the following FirebaseError
when interacting with the instance after hot reloading or hot restarting:
FirebaseError: initializeFirestore() has already been called with different options. To avoid this error, call initializeFirestore() with the same options as when it was originally called, or call getFirestore() to return the already initialized instance.
Steps to reproduce
- Create a Flutter web project and initialize a Firebase app with Firestore.
- Modify the Firestore instance's settings in some way, like setting
ignoreUndefinedProperties
totrue
. - Perform some operation that utilizes the instance, like a document get. No exceptions occur.
- Hot restart or reload the app.
- Perform the operation from step 3 again. The aforementioned exception is thrown.
Expected behavior
- Create a Flutter web project and initialize a Firebase app with Firestore.
- Modify the Firestore instance's settings in some way, like setting
ignoreUndefinedProperties
totrue
. - Perform some operation that utilizes the instance, like a document get. No exceptions occur.
- Hot restart or reload the app.
- Perform the operation from step 3 again. No exception should be thrown.
Sample project
Repo for the project can be found here. And its main.dart
:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firestore_settings_error/firebase_options.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
FirebaseFirestore.instance.settings = const Settings(
ignoreUndefinedProperties: true,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firestore Settings Error',
home: Scaffold(
body: Center(
child: TextButton(
child: const Text('Click me'),
onPressed: () async {
final doc = await FirebaseFirestore.instance
.collection('testing')
.doc('cM5HVGdmTLdLaQVEs9Ek')
.get();
print(doc.data());
},
),
),
),
);
}
}
Additional context
When poking around the flutterfire
repo to try to solve a separate issue, I noticed that the FirebaseFirestore.instance.settings
getter seemed to always return a new instantiation of Settings
. I'm not sure if it's still the case, or if I was misunderstanding, but at the time it seemed like the modified settings were set once on initialization, passed to the wrapped JS SDK, and then Flutter code knew nothing of it after that fact.
(Edit) Found the spot in code here:
@override
Settings get settings {
return const Settings();
}
I'm wondering if this may be contributing. Is there a reason that this is done?
Flutter doctor
Click To Expand
[✓] Flutter (Channel stable, 3.16.9, on macOS 14.3 23D56 darwin-x64, locale
en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] VS Code (version 1.86.1)
[✓] Connected device (2 available)
[✓] Network resources
Flutter dependencies
Click To Expand
Dart SDK 3.2.6
Flutter SDK 3.16.9
firestore_settings_error 1.0.0+1
dependencies:
- cloud_firestore 4.15.4 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta]
- cupertino_icons 1.0.6
- firebase_core 2.25.4 [firebase_core_platform_interface firebase_core_web flutter meta]
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math web sky_engine]
dev dependencies:
- flutter_lints 2.0.3 [lints]
- flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math async boolean_selector characters collection material_color_utilities meta source_span stream_channel string_scanner term_glyph web]
transitive dependencies:
- _flutterfire_internals 1.3.21 [collection firebase_core firebase_core_platform_interface flutter meta]
- async 2.11.0 [collection meta]
- boolean_selector 2.1.1 [source_span string_scanner]
- characters 1.3.0
- clock 1.1.1
- cloud_firestore_platform_interface 6.1.5 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface]
- cloud_firestore_web 3.10.4 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js]
- collection 1.18.0
- fake_async 1.3.1 [clock collection]
- firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface]
- firebase_core_web 2.11.4 [firebase_core_platform_interface flutter flutter_web_plugins js meta web]
- flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math web]
- js 0.6.7 [meta]
- lints 2.1.1
- matcher 0.12.16 [async meta stack_trace term_glyph test_api]
- material_color_utilities 0.5.0 [collection]
- meta 1.10.0
- path 1.8.3
- plugin_platform_interface 2.1.8 [meta]
- sky_engine 0.0.99
- source_span 1.10.0 [collection path term_glyph]
- stack_trace 1.11.1 [path]
- stream_channel 2.1.2 [async]
- string_scanner 1.2.0 [source_span]
- term_glyph 1.2.1
- test_api 0.6.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph]
- vector_math 2.1.4
- web 0.3.0
Thanks for the report. I was able to replicate the reported behavior using details provided.
@jcwasher Was this solved for you? I am still getting it. I'm on the latest version of cloud_firestore 4.15.8
@nialljawad96 Sorry for late reply, I was out of town. I didn't get a chance to check on 4.15.8
but working for me on 4.15.9
:
dependencies:
flutter:
sdk: flutter
cloud_firestore: 4.15.9
firebase_core: 2.27.1