Reputation: 806
I am trying to build a flutter web app. I using the Firebase emulator to test the app. So when I Hot Refresh, I get the error in the title along with:
FirebaseError: Firestore has already been started and its settings can no longer be changed. You can only modify settings before calling any other methods on a Firestore object.
My main()
:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
String host = 'localhost';
if (isEmulator) {
if (!kIsWeb) {
host = Platform.isAndroid ? '10.0.2.2' : host;
}
await FirebaseAuth.instance.useAuthEmulator(host, 9099);
try {
FirebaseFirestore.instance.useFirestoreEmulator(host, 8080);
} on Exception catch(e){
debugPrint('continue');
}
await fb_storage.FirebaseStorage.instance.useStorageEmulator(host, 9199);
//FirebaseFunctions.instance.useFunctionsEmulator(host, 5001);
}
runApp(const MyApp());
}
I also wrapped useFirestoreEmulator()
in a try-catch. But the dart2js transformation does not consider the FirebaseError
in Javascript as an exception. So I do not have a way to divert it.
my flutter doctor:
[√] Flutter (Channel stable, 2.8.0, on Microsoft Windows [Version 10.0.19044.1415], locale en-US)
• Flutter version 2.8.0 at C:\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision cf44000065 (2 weeks ago), 2021-12-08 14:06:50 -0800
• Engine revision 40a99c5951
• Dart version 2.15.0
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:\Users\muddi\AppData\Local\Android\sdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:\Program Files\Android\Android Studio1\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Android Studio (version 2020.3)
• Android Studio at C:\Program Files\Android\Android Studio1
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
[√] VS Code (version 1.63.2)
• VS Code at C:\Users\muddi\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.29.0
[√] Connected device (2 available)
• Chrome (web) • chrome • web-javascript • Google Chrome 96.0.4664.110
• Edge (web) • edge • web-javascript • Microsoft Edge 96.0.1054.62
• No issues found!
Upvotes: 0
Views: 605
Reputation: 1207
Reviewing for information related to the error message you are getting, I found a solution that is to call useEmulator after enablePersistence to fix the "Firestore has already been started" issue.
Now, you will need to make sure that all emulators have started before making a request.
I also found a similar question in Stackoverflow that contains two answers with a solution that would probably help with the error message, one solution is also using a Hot Refresh
.
Upvotes: 1