In my Flutter app I want to access remote config in the AppDelegate of the Swift Native part of the App.
I am unable to find guidelines on the best way to do this
I have tried calling, in the app delegate
FirebaseApp.configure()
and in my main.dart
await Firebase.initializeApp
But this seems to give me an error
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
#0 FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:210:7)
<asynchronous suspension>
#1 MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:44)
<asynchronous suspension>
#2 MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:7)
<asynchronous suspension>
#3 Firebase.initializeApp (package:firebase_core/src/firebase.dart:66:31)
<asynchronous suspension>
#4 main.<anonymous closure> (package:tempdrop_ovuview_flutter_module/main.dart:31:18)
<asynchronous suspension>
#5 main (package:tempdrop_ovuview_flutter_module/main.dart:27:3)
<asynchronous suspension>
If I then do a hot restart everything works as expected. I have not tried an Android implementation yet.
What are the guidelines to use Firebase in Both Flutter and Native?
2
Answers
You should call
WidgetsFlutterBinding.ensureInitialized();
beforeawait Firebase.initializeApp
.Here is the full documentation link for
Firebase
withFlutter
.https://firebase.google.com/docs/flutter/setup?platform=ios
I would assume this issue is caused by initializing the native
FirebaseApp
twice. In this case, I can suggest two approaches:Firebase.initializeApp()
should initialize the underlying native SDK as well. You mentioned having to use it inAppDelegate
– if by that you mean that you need to use it at startup of the app, you could try to defer it after Flutter is initialized (e.g. by triggering the Swift logic using aMethodChannel
just after Dart’s call toinitializeApp
completes).The same could be done on the Swift side as well.