how to setup Flutter Firebase for desktop application - flutter

How do I set flutter to work with firebase functions like firestore, storage, and authentication?
Firebase.initializeApp()

Your question is not clear but if you are asking how to add firebase dependencies; you can get packages from pub.dev.
And in any one of them; click the installing tab and use this code: flutter pub add xxx(depends on the package you use) for example for firebase auth:
flutter pub add firebase_auth
Do not add dependencies directly to pubspec.yaml because it will make some mismatch with other packages. when you use flutter pub add xxx you don't get any problem with the harmony of dependencies.
Addition:
If you're asking how to use Firebase.initializeApp() you need to call it where you will use Firebase. For example ;
Future<void> initializeDefault() async {
FirebaseApp app = await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print('Initialized default app $app');
}

Related

Trying to run app on flutter web throws exception [Error: Assertion failed:] [duplicate]

I am using the flutter course "Get to know Firebase for Flutter" from https://firebase.google.com/codelabs/firebase-get-to-know-flutter#4.
I am in step_02 and I have added the following recommended code from stage 5.
import 'package:firebase_auth/firebase_auth.dart'; // new
import 'package:firebase_core/firebase_core.dart'; // new
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart'; // new
import 'firebase_options.dart'; // new
import 'src/authentication.dart'; // new
import 'src/widgets.dart';
Later in this stage there is a Test it section. However it fails because there is no firebase_options.dart file. How do I generate this file.
Thank you.
Previously you had to download the google-service.json and GoogleService-Info.plist from the Firebase console and place them in the android and ios folders in your Flutter app.
Starting with Flutter 2.8, there is a new way to initialize a Firebase project within Flutter to bypass this step.
Create project in Firebase console, but you don't need to download the files mentioned or change build.gradle files
Install Firebase CLI here
run dart pub global activate flutterfire_cli in your Flutter project
run flutterfire configure
This will start a command line interface for you to select the Firebase projects you want to link to the Flutter project. After you complete this, a firebase_options.dart file will be generated in your lib/ folder.
Finally, to initialize Firebase in your main.dart:
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(MyApp());
}
PD: After updating to the new initialization method, Crashlytics and Analytics stopped working for me and I had to revert to the old initialization method. I don't think this new method is quite there yet. Guide for the old method.
The firebase_option file automatically generates after Flutter successfully configures your firebase project with your flutter app. For Android, make sure you've added the google-services.json file to your Android>app root directory and for ios, GoogleService-info.plist file into the root of your Xcode project as well as all targets.
If you're still having problems, I'd suggest using the Firebase CLI direct from the terminal to configure your firebase project.
From your project root terminal, command:
$ flutterfire configure
// This requires the Firebase CLI to work.
Select firebase project by hitting return or enter. Next you'll be asked to select which platforms the configuration should support, e.g. android, ios, web. If you haven't created some of these in the firebase console, don't worry as it will create and register it for you in this step and update the android build.gradle files.
** Proceed to step 4 if you already have the firebase_core plugin installed. **
Install the latest version of the firebase_core plugin by running this command from your project root directory:
$ flutter pub add firebase_core
Add imports to the main file:
import 'package:firebase_core/firebase_core.dart'; //
import 'firebase_options.dart'; // Generated file
Update your main function to initialize firebase with this async function:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options:
DefaultFirebaseOptions.currentPlatform);
runApp(const YourAppName());
}
Delete the google-services.json and google.plist files if you had these installed before.
$ flutter clean
$ flutter run
See the FlutterFire documentation for more information.
After completing the instructions provided by Bugzilla, I was able to locate the firebase_options.dart file in the lib directory. I changed the path of the import from 'firebase_options.dart' to '../firebase_options.dart' and it worked for me.
Solved this by removing my existing Firebase project and creating a new one, disabling Google Analytics.

Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)

i got this Error i didn't know how i would to solve it any solution Please.
when i initialise firebase im main function it show me this white screnn and flutter icon:
but when i delete this initliation this firebase app run successfully as it is:
and show me those errors
Make sure you have firebase_core in your pubspec.yaml
Then, just use the following in your main:
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
run
flutter clean
and
flutter pub get
in your terminal.
If it still fails, please go step by step following the firebase documentation. It should work. https://firebase.google.com/docs/flutter/setup?platform=android
I just forget to add Google services plugin in android/app/build.gradle:
apply plugin: 'com.google.gms.google-services'

firebase_performance - Default FirebaseApp is not initialized - FlutterFire

I have a flutter project with Android, iOS and Web platforms enabled, I'm using the following firebase plugins:
firebase_performance: ^0.8.2+1
firebase_database: ^9.0.20
cloud_firestore: ^3.4.1
firebase_core: ^1.20.0
All of them are added usgin FlutterFire. This is my main file:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
initializeDateFormatting('es');
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
.....
}
When i remove firebase_perfomance the app works fine, but when i add this plugin the app return the folliwing error message:
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.testapp.test_performance. Make sure to call FirebaseApp.initializeApp(Context) first.
I haven't google-services.json beacause I understand that it is not necessary if I use flutterfire
I run into same problem today :(
It's weird that seems Firebase App won't auto initialized before the flutter startup, if you setup the firebase plugin with flutterfire cli. And no one mentioned this problem.
Solution:
You can follow the https://firebase.google.com/docs/android/setup to setup the google services plugin,the firebase service seems would initialized to get the performance monitor works in early stage of APP.
I have the same issue with firebase_performance package. I solved the issue by running the following commands again.
dart pub global activate flutterfire_cli
flutterfire configure

Calling Firebase.initializeApp() returns 'Unable to establish connection on channel' - Flutter + Firebase

I'm working with Firebase in flutter (latest versions as of 1st July 2022), and when I try to call Firebase.initializeApp() in my Main function, it returns an error of:
'[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)'
Here is my code:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Firebase.initializeApp();
runApp(const MyApp());
}
Any help would be greatly appreciated
I was able to solve the problem by upgrading all my firebase dependencies to the latest version using flutter pub outdated and flutter pub upgrade firebase_....
I encountered this issue after adding the firebase_messaging plugin v11.4.4. Might be related to the change in firebase_core_platform_interface.
Also got it after a pub update. Fixed it by doing:
flutter clean
and removing the pubspec.lock
Do a flutter clean and upgrade firebase core with
flutter pub upgrade firebase_core
The run
dart pub global activate flutterfire_cli
flutterfire configure
to reinitialize firebase. I experienced this after adding app_check support
I have updated the below dependency:
dependency_overrides:
firebase_core_platform_interface: 4.4.1
Executed this command and updated firebase dependencies
flutter pub upgrade firebase_core
flutter pub upgrade firebase_messaging
it is working properly now.
Problem seems to be with the core platform interface.
Add firebase_core_platform_interface: 4.4.0 and adjust all other firebase deps to make them compatible with this library. I downgraded all versions to 36 day old release and it finally works.
I just built a new flutter app and I got this error when initializing Firebase App and running it in the browser and not the android emulator. I am using VS Code and you can change the target device in the bottom task bar on the right side.
Solution is to run it in the android emulator. I don't know if there are permissions to be set for web view but I do not plan on deploying to web.
Had the same problem and scaled back (thank you git) - getting flutter dependencies right - as in it compiles and work as advertised is no mean feat. Welcome :)

Flutter Error : No Firebase App ‘[DEFAULT]’ has been created – call Firebase.initializeApp()

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firestore.initializeApp();
runApp(BookApp());
}
I used these codes but firestore is not recognized in main.dart
I am using latest versions
cloud_firestore: ^1.0.1
firebase_core: ^1.0.1
Check for the following, if you have missed some steps:
Create a firebase project in the firebase console.
Make sure the bundleID of the app matches with the bundle ID of the project created in Console.
Android: Make sure you have copied the google-services.json file to the Android>app directory (copy item if needed)
iOS: Make sure you have copied the GoogleServices-Info.plist file to iOS>Runner directory.
try flutter clean and restart the app
You need to intialize FlutterFire first by calling await Firebase.initializeApp(); before using any Firebase services.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await Firestore.initializeApp();
runApp(BookApp()
}