Friday, 14 October 2022

How to utilize Firebase both in Flutter as well as native android within the same project?

I have a project in flutter that relies on Firebase for authentication, firestore, and functions. However, I have a receiver class that extends BroadcastReceiver and runs in the background, on the android side of my application. Within this receiver class I utilize Firestore, Analytics, Auth and Cloud_Functions.

Given this unique android setup, I need to instantiate the following dependencies in my app level build.gradle file:

implementation 'com.google.firebase:firebase-firestore:24.3.1'
    implementation 'com.google.firebase:firebase-auth:21.0.8'
    implementation 'com.google.firebase:firebase-firestore-ktx:24.3.1'
    implementation 'com.google.firebase:firebase-messaging:23.0.8'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-functions-ktx:20.1.1'
    implementation 'com.google.firebase:firebase-analytics-ktx:21.1.1'
    implementation platform('com.google.firebase:firebase-bom:30.4.0')
    implementation 'com.google.firebase:firebase-auth-ktx'

I also have the following in my Flutter pubspec.yaml file:

firebase_core: ^1.24.0
  cloud_firestore: ^3.5.1
  firebase_messaging: ^13.1.0
  cloud_functions: ^3.3.9
  firebase_auth: ^3.11.2

as the Flutter project itself handles a lot of other things as well that are dependent on Firebase (shared components between iOS and Android that use these libraries).

I've tested my app on just iOS and it builds and runs fine since I don't use any kind of iOS specific classes in the background, and I've also run the android app by itself without flutter and it builds and runs fine. But when I bring those native android classes into my flutter project, I get runtime errors when spinning my app up for android:

E/flutter (15469): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter (15469): #0      FirebaseCoreHostApi.initializeCore
package:firebase_core_platform_interface/…/pigeon/messages.pigeon.dart ...

in reference to the following line of code:

WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

I'm still able to run the iOS side of the app fine, and all dependencies are up to date for me, so I'm fairly confident the issue stems from me having the same packages installed within my app.build.gradle file.

Note: If I remove those dependencies in my gradle file, then the app breaks and throws errors immediately as it can't find those libraries anywhere.

My question is: How can I implement these firebase packages and reference them from within a native Kotlin file found in the android section of my Flutter project?

I suspect I'm just not searching for the right thing, but everything online just says "Update your packages in your pubspec.yaml file, and mine are already updated so those responses aren't helpful at all.



from How to utilize Firebase both in Flutter as well as native android within the same project?

No comments:

Post a Comment