I created a new Flutter
app with the command flutter create test9_firebase_setup -a kotlin --platforms android
. The goal is to connect to firebase with auth, analytics and crashlytics working.
In my pubspec.yaml
, I have added:
dependencies:
# ...
firebase_core: ^2.17.0
firebase_analytics: ^10.5.1
firebase_crashlytics: ^3.3.7
firebase_auth: ^4.10.1
My main() function looks like that. After initialization of firebase, we sign in anonymously just to check that it works. The result of the authentication is printed in the console. Then we initiate Crashlytics and we log an entry in Analytics. Those snippets are coming straight from the doc, so they should work.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
final user = await FirebaseAuth.instance.signInAnonymously();
print(
user.user?.isAnonymous == true ? 'You are signed in' : 'Can\'t sign in',
);
FlutterError.onError = (errorDetails) {
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
};
PlatformDispatcher.instance.onError = (error, stack) {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};
await FirebaseAnalytics.instance.logBeginCheckout(
value: 10.0,
currency: 'USD',
items: [
AnalyticsEventItem(
itemName: 'Socks',
itemId: 'xjw73ndnw',
price: 10.0,
),
],
coupon: '10PERCENTOFF',
);
runApp(const MyApp());
}
I enabled Authentication, Crashlytics and Analytics in my firebase console. And then I ran flutterfire configure
. This created the firebase_options.dart
and the google-services.json
.
I'd like to point out that I skipped the SDK instructions given by firebase during the configuration of the android app in the console, because (as I understood from some third-party tutorials I followed) these steps should be automatically done by flutterfire configure
. Therefore I did not do any manual modification of build.gradle
at this point.
When I run my app, I can see the You are signed in
line in my console. And indeed I can see a new anonymous user in my console on the web. So that means that the connection with Firebase works.
I saw several tutorials and articles on the web were people were running flutterfire configure
and everything was working. But apparently not for me, because I get the Missing google_app_id. Firebase Analytics disabled
error in my console.
I tried triggering an Exception in my UI but the Crashlytics dashboard did not show anything.
Then I updated my build.gradle file manually. I added the following lines in the app level build.gradle
file:
dependencies {
implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
implementation("com.google.firebase:firebase-analytics-ktx")
implementation("com.google.firebase:firebase-auth-ktx")
}
Now, crashlytics seems to be working, but I still get the Missing google_app_id. Firebase Analytics disabled.
error. And in fact my 'Realtime Analytics' and 'Analytics Dashboard' pages on the firebase console do not show any activity.
I went back to the SDK Instructions given by Firebase during the project setup and I tried to do them manually. My project was setup with kotlin and they tell me to look for a build.gradle.kts
file. But I only have build.gradle
files. I tried anyway:
- I don't have any
plugins {}
in my root-level buildgradle file, so I tried adding this but then I get the errorPlugin [id: 'com.google.gms.google-services', version: '4.4.0', apply: false] was not found in any of the following sources: [...]
- The same error is obtained if I add in my app-level build.gradle file the line below. The
dependencies
section was already filled out (as said above) and is working.
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
// Add this:
id "com.google.gms.google-services"
}
So, 3 questions:
- Why do I have
build.gradle
files and notbuild.gradle.kts
files even though my flutter project was setup with kotlin? - Shouldn't
flutterfire configure
automatically updatebuild.gradle
and add the required dependencies? - Can anyone recommend steps to enable Google Analytics?
Thanks!
4.3.14
! Version4.4.0
simply didn't compile for me. – Injection