1

I am very new to Dart, and coding in general. I have produced this code after watching tutorials on YouTube. For the most part, I have been able to troubleshoot most of my problems on my own, yet I cannot figure out my most recent errors. I have using firebase for authentication as soon as I write code in main.dart

Future <void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp().then((value) => Get.put(AuthController()));
runApp(const MyApp());
}

and if i run the app, I get the following error

E/flutter ( 5661): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 
[core/not-initialized] Firebase has not been correctly initialized.
E/flutter ( 5661): 
E/flutter ( 5661): Usually this means you've attempted to use a Firebase service before 
calling `Firebase.initializeApp`.
E/flutter ( 5661): 
E/flutter ( 5661): View the documentation for more information: 
https://firebase.flutter.dev/docs/overview#initialization
E/flutter ( 5661):     
E/flutter ( 5661): #0      MethodChannelFirebase.initializeApp  (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:99:9)
E/flutter ( 5661): <asynchronous suspension>
E/flutter ( 5661): #1      Firebase.initializeApp 
(package:firebase_core/src/firebase.dart:40:31)
E/flutter ( 5661): <asynchronous suspension>
E/flutter ( 5661): #2      main (package:definer_lms/main.dart:10:3)
E/flutter ( 5661): <asynchronous suspension>
E/flutter ( 5661): 

My android/app/build.gradle has the following config:

defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "getin.******.definer_lms" // edited for security purpose
    minSdkVersion 23
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    multiDexEnabled true
}
.....
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:29.1.0')
implementation 'com.android.support:multidex:1.0.3'
}

.....
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'

}

My android/build.gradle has the following config:

dependencies {
    classpath 'com.android.tools.build:gradle:4.1.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.google.gms:google-services:4.3.10'
}

can someone please find what is wrong here?

sandeep rana
  • 121
  • 11

1 Answers1

0

You need to write this way :

  WidgetsFlutterBinding.ensureInitialized();
  
  await Firebase.initializeApp().then((value)  {
      
    Get.put(AuthController());
    
    runApp(const MyApp());
  });
 
Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14
  • Thank You. i tried it but its giving same error – sandeep rana Mar 03 '22 at 12:43
  • can you share the error – Hardik Mehta Mar 03 '22 at 12:45
  • 'Syncing files to device Android SDK built for x86... E/flutter ( 9634): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized. E/flutter ( 9634): E/flutter ( 9634): Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`. E/flutter ( 9634): E/flutter ( 9634): View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization E/flutter ( 9634): – sandeep rana Mar 03 '22 at 12:47
  • you can refer this link : https://stackoverflow.com/questions/63804012/flutter-firebase-and-android-issue-unable-to-initialise-cannot-find-google-se – Hardik Mehta Mar 03 '22 at 12:49
  • I tried everything from that link before I posted the question. Still the same. – sandeep rana Mar 03 '22 at 12:55
  • try to clean , run and re check – Hardik Mehta Mar 03 '22 at 13:24
  • I reinstalled Android Studio, and this worked for me. Thanks for the Help. – sandeep rana Mar 04 '22 at 03:39