Flutter: Firebase has not been correctly initialized - firebase

I'm working on iPhone 12 Pro Max Emulator, macOS Catalina.
I'm getting this error when I try to run the app:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
Also there is a tip in the console:
Usually this means you've attempted to use a Firebase service before calling Firebase.initializeApp.
I initialize the Firebase before use it. Like this:
void main() async {
print('-- main');
WidgetsFlutterBinding.ensureInitialized();
print('-- WidgetsFlutterBinding.ensureInitialized');
await Firebase.initializeApp();
print('-- main: Firebase.initializeApp');
runApp(const MyApp());
}
This is what I see in the console output:
Xcode build done. 132.9s
flutter: -- main
flutter: -- WidgetsFlutterBinding.ensureInitialized
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`.
I can't see the -- main: Firebase.initializeApp line in the console. So it fails in first trying to initialize the Firebase.
I create Android/Apple apps in Firebase. Downloaded google-services.json / GoogleService-Info.plist and put in the project.
GoogleService-Info.plist:
google-services.json:
I'm not using the android, but I added dependency into build.gradle: classpath 'com.google.gms:google-services:4.3.10'
And app/build.gradle: apply plugin: 'com.google.gms.google-services'
dependencies:
firebase_auth: ^3.3.5
firebase_messaging: ^10.0.9
google_sign_in: ^5.2.1
flutter --version:
Flutter 2.5.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18116933e7 (3 months ago) • 2021-10-15 10:46:35 -0700
Engine • revision d3ea636dc5
Tools • Dart 2.14.4
How can I solve this problem? BTW, I'm working on a brand new flutter project.

When you add google-services.json to an iOS project, you need to add it using Xcode as described in the following document:
https://firebase.flutter.dev/docs/manual-installation/ios
If you read through the page, you'll find the following note:
adding [google-service.json] manually via the filesystem won't link the file to the project
You need to try that then restart your app (rebuild it).
Edit: Additional Note:
You'll also need to add firebase_core to your dependencies in pubspec.yaml.

You have to add the GoogleService-Info.plist in Xcode instead of just placing it into the folder:
So it looks like this:
Note: The file can also be moved to a sub folder in the project (i.e. Runner).
Source: https://www.kindacode.com/article/flutter-correctly-adding-googleservices-info-plist-to-ios/

Here's how I fixed this error:
Ensure that all firebase services have been added to your pubspec.yaml file, in the dependencies section. firebase_core appears to be missing and is required to connect your flutter app to your firebase project.
You can simply add it using this command:
flutter pub add firebase_core
Add the firebase plugins to your main file:
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
Replace your void main function with an asynchronous one:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const YourAppGoesHere());
}

When I add the GoogleService-Info.plist file in Xcode, I used the wrong name GoogleService-Info**(1)**.plist.
If you have the same file in downloads, mac adds a number of copy to the next downloaded file.

Related

Flutter Compile Error on Firebase Auth: "A non-null value must be returned since the return type 'Never' doesn't allow null."

I'm using flutter fire and already did the console configuration, but everytime I try to run the code with an firebase_auth import I got this error.
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth_platform_interface-6.2.0/lib/src/method_channel/utils/exception.dart:12:7: Error: A non-null value must be returned since the return type 'Never' doesn't allow null.
The error message was added from dart 2.16, i.e from flutter 2.10, so the primary solution will be to upgrade your flutter version, which will also upgrade dart version.
So I ran into this problem recently while using firebase_storage
What I did to solve the problem was... (I was on flutter 2.5 and dart 2.14).
Upgrade flutter and dart version: Into terminal, run flutter upgrade or in my case, flutter upgrade --force, since flutter upgrade was giving me some issues.
Then I added the latest version of the dependencies to my pubspec (firebase_storage_10.2.0)
At this stage, running the app will likely throw an error about unsupported compileSDKVersion or so, and ask you to upgrade.
For that, go into your app level build.gradle file
("project"\android\app\build.gradle),
and under android, change the compileSDKVersion from it's current value to what flutter asks you to change to. in my case, it was from 30 to 31
android{
//change compile sdk version to 31 (in my case. flutter will tell you which version you should set to)
compileSdkVersion 31
}
When you run the app now, it will check for licenses for the Android SDK Package you just edited (31). it should automatically accept and install everything neccessary.
Flutter run might then fail again, with the error of an incompatible kotlin version, and that you should update or so.
To update the kotlin version, go to the project level build.gradle ("project"/android/build.gradle)
change the kotlin version here (ext.kotlin_version = '1.3.5') to the latest version which can be found Here. as of now, the latest version is 1.6.10
so this line of code now reads
ext.kotlin_version = '1.6.10'
Now you are good to go. run the app again, it might take much longer than usual, but it should work just fine.
Or at least it worked fine for me.
I also had the same error.
Try upgrading the dart version or use lower version of firebase_auth.
firebase_auth: 2.0.0
firebase_auth: 2.0.0 worked for me.
Overall Complete Solution.
Solution:
Step 1:
The solution for this error is to upgrade to new flutter version.
Open Command Prompt and run this command.
flutter upgrade
Then after upgrading to new flutter version. Open your project and test it on your phone.
If shows error to upgrade kotlin version also, then:
Step 2:
As we have upgrade to new version of flutter so we have to upgrader things also in build.gradle.
-> Go to android/build.gradle
-> change to ext.kotlin_version = '1.6.10'
For example,
In my old version i had  ext.kotlin_version = '1.3.50' and now i change it to new  ext.kotlin_version = '1.6.10'
Please Check here the Example in Screenshot Image
Now Today Date is: 01/3/2022
In future: if you are reading this comment, then the version number ext.kotlin_version = '1.6.10' may be different.
So For latest new version number, go to this site:
https://kotlinlang.org/docs/gradle.html#plugin-and-versions
Here is the ScreenShot for Understanding
Step: 3
-> Go to android/app/build.gradle
-> change minSdkVersion to 19
-> add multiDexEnabled true
For example,
Check here the example Screenshot
Now you can run and test the app on your phone. It will work 100 percent.

MissingPluginException exception when a firebase anonymous sign-in with flutter (windows) app

I recently started out with flutter for windows. I'm following this firebase and flutter tutorial. At lecture 4, I am getting error with Firebase Auth:
flutter: MissingPluginException(No implementation found for method signInAnonymously on channel plugins.flutter.io/firebase_auth)
I think the problem is because I am building for windows. I don't know how to add firebase to windows application. Any help is appreciated
Here is the complete log:
Launching lib\main.dart on Windows in debug mode...
Building Windows application...
Waiting for Windows to report its views...
Debug service listening on ws://127.0.0.1:60688/97Ok8iT1Hjo=/ws
Syncing files to device Windows...
flutter: MissingPluginException(No implementation found for method signInAnonymously on channel plugins.flutter.io/firebase_auth)
flutter: error signing in
EDIT 1
pubspec.yaml file (dependencies section)
dependencies:
flutter:
sdk: flutter
firebase_auth: ^0.14.0+5
cloud_firestore: ^0.12.9+4
EDIT 2
I updated the dependencies to use following versions:
firebase_auth: ^0.18.1+2
cloud_firestore: ^0.14.1+3
firebase_core: ^0.5.0+1
But now I am getting the following error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)
This is what my main function looks like:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Late to the party, but the actual issue is that flutter_core does not actually support Windows.
It only supports Android, iOS, MacOS & Web (See the firebase_core package on pub.dev).
You are just hot reloading or hot restarting your flutter after adding the await Firebase.initializeApp(); in your void main() function.
Just Stop your main.dart process and run it again from the begining -- thats it, now your app gets integrated with firebase!
note: during running some may face issues with the Multidex error refer link: D8: Cannot fit requested classes in a single dex file (# methods: 71610 > 65536) to solve the error or just add:
in your project level >> android >> app >> build.gradle :
defaultConfig {
...
multiDexEnabled true
}
incase your running the app on android and your MainActivity has this import statement:
import io.flutter.app.FlutterActivity
you might have to change it to this instead:
import io.flutter.embedding.android.FlutterActivity
make sure you enable signin method in console firebase
https://console.firebase.google.com
But now I am getting the following error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method Firebase#initializeCore on channel plugins.flutter.io/firebase_core)
As mentioned in here, You need to set com.android.tools.build:gradle:3.5.0 in your dependencies in android/build.gradle.
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
// ...
}

FIREBASE FOR FLUTTER CodeLab not working on IOS sim

I'm pretty new to Flutter and want to add Firebase to my Flutter app. So I went through the Flutter Codelab:
enter link description here
However I am getting errors as soon as I add the Cloud_Firestore package to my Pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
cloud_firestore: ^0.8.2 # new
I continued hoping that a following activity would resolve the problem and created the GoogleService-Info.plist file and added that as instructed.
However I still get the errors and build fail.
Here are the errors:
Error output from Xcode build: ↳ 2019-01-21 10:19:17.576
xcodebuild[70286:12860097] [MT] PluginLoading: Required plug-in
compatibility UUID D7881182-AD00-4C36-A94D-F45FC9B0CF85 for plug-in at
path '~/Library/Application
Support/Developer/Shared/Xcode/Plug-ins/RealmPlugin.xcplugin' not
present in DVTPlugInCompatibilityUUIDs 2019-01-21 10:19:17.577
xcodebuild[70286:12860097] [MT] PluginLoading: Required plug-in
compatibility UUID D7881182-AD00-4C36-A94D-F45FC9B0CF85 for plug-in at
path '~/Library/Application
Support/Developer/Shared/Xcode/Plug-ins/Alcatraz.xcplugin' not present
in DVTPlugInCompatibilityUUIDs
** BUILD FAILED **
Any help with solving this would be really appreciated
Thanks
Ok so I found the answer. Not sure why it's happening, seems that it's due to a bug in cloud_firestore 0.8.0.
So if anyone else is having this error, do the following:
go to terminal, open your apps directory,
cd ios
pod update Firebase
That should fix it.

flutter version conflict with location and firebase_messaging

I am using two plugin with my flutter app location: ^1.4.1 and firebase_messaging: ^2.0.0
If I use single one its works fine but together
What went wrong:
Failed to capture snapshot of input files for task ':app:preDebugBuild' property 'compileManifests' during up-to-date check.
> The library com.google.android.gms:play-services-basement is being requested by various other libraries at [[15.0.1,15.0.1]], but resolves to 16.0.1. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
flutter clean
trying to downgrade version for both plugin
and google search
non of the above works for me.
app level build: implementation 'com.google.firebase:firebase-core:16.0.1'
project level build: classpath 'com.google.gms:google-services:4.0.1'
Thanks
:)
In your Project - Goto Android > App > build.gradle
Right after the apply plugin: 'com.google.gms.google-services' at the bottom of your build.gradle the following can be added to work around the issue.
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

The Target URI doesn't Exist:'package:firebase_database/firebase_database.dart';

Importing Dependencies in Pubsec.Yaml is successfull,
But when opening main.dart file it shows the error
Problem(1)
The Target URI doesn't Exist:
'package:firebase_database/firebase_database.dart';
Either
flutter packages get
needs to be run or failed when you run it
or you just need to restart your IDE. I saw it mentioned a few times recently that this was necessary to fix the problem.
1- Go to the link: https://pub.dev/packages/firebase_database#-installing-tab-
2- Add dependencies in pubspec.yaml file.e.g:-
dependencies:
flutter:
sdk: flutter
firebase_database: ^3.1.3
3- Press ctrl+s after two seconds it will give a message click on Get Dependencies, and wait for some time its will run pub get in your project.
4- Problem will be solved.

Resources