skip to Main Content

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


  1. You should call WidgetsFlutterBinding.ensureInitialized(); before await Firebase.initializeApp.

    Here is the full documentation link for Firebase with Flutter.

    https://firebase.google.com/docs/flutter/setup?platform=ios

    Login or Signup to reply.
  2. I would assume this issue is caused by initializing the native FirebaseApp twice. In this case, I can suggest two approaches:

    1. Initialize Firebase only on the Flutter side, before you use it in Swift. Dart’s Firebase.initializeApp() should initialize the underlying native SDK as well. You mentioned having to use it in AppDelegate – 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 a MethodChannel just after Dart’s call to initializeApp completes).
    2. Initialize one of the sides using a different name. Firebase SDK’s allow initializing multiple apps under different names. So assuming you want to change the app name on the Flutter side:
    // In main()
    await Firebase.initializeApp(name: 'Flutter');
    
    // Later 
    final app = Firebase.app('Flutter');
    final auth = FirebaseAuth.instanceFor(app: app)
    final firestore = FirebaseFirestore.instanceFor(app: app);
    // Same for other firebase services
    

    The same could be done on the Swift side as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search