Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How do I use FlutterFire with a emulator demo project?

Posted on November 11, 2022 By

Solution 1 :

My current app has AppConfig class that extends InheritedWidget that keeps instance available when in use.

You can try with

class AppConfig extends InheritedWidget {

    static AppConfig? of(BuildContext context) {
        initializeFirebase();
        return context.dependOnInheritedWidgetOfExactType<AppConfig>();
      }
    
    static Future<void> initializeFirebase() async {
        try {
          await Firebase.initializeApp();
        } catch (e) {}
      }
}

You need to initialise the AppConfig() in main()

Solution 2 :

  1. Try to change localhost to your machine ip, in both firebase command line and your app. Some simulators, such as ios simulator, cannot understand localhost.
  2. Please paste a fully reproducible sample, instead of just part of the lines.
  3. Try to await Firebase.initializeApp(); inside main function, not initState
  4. Firstly check at port 8080 manually and see whether the firebase server really works

Solution 3 :

Technically, the problem is not with the emulator. There are two problems with the code here.

Problem 1: Incorrect connection to the emulator

Providing ‘localhost:8080’ to the host argument for Firestore Settings is not asking FlutterFire to use the emulator. The right way to use the emulator for Firestore is

FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);

You could wrap that call in an if statement checked against !kReleaseMode so that you won’t need to comment it out when you want to build the application. Remember to import flutter/foundation.dart.

Problem 2: No FirebaseOptions provided

The following error:

E/flutter ( 7724): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.

is caused from your Firebase.initializeApp() statement.

Firebase.initializeApp() takes an optional named options argument whose value is a type of FirebaseOptions (with parameters like apiKey, appId, projectId, …), and you are not providing it.

The FirebaseOptions for each platform (android, ios, and web) are available in the Firebase console of a given project. Firebase can’t be initialized for any client without its own config or FirebaseOptions.

To setup Firebase in a Flutter project, you need to run the flutterfire configure command in that flutter project folder. The command will ask you to choose a project from the Firebase console. The command will then fetch configs or FirebaseOptions for each platform and make them available through some switch statements for the currentPlatform in a firebase_options.dart file.

READ  [FIXED] java - My NFC Scan application starts only if NFC Type 2
Powered by Inline Related Posts

You then have to import this file in main.dart and call initializeApp with the option for the given platform as according to the docs:

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

Note that the command can also create apps for you in the Firebase project, if you haven’t yet created them.

Let’s dive deeper

The options parameter is optional, but it is technically required. If you look at the source code for Firebase.initializeApp(), you will see that under the hood, it invokes MethodChannel calls for the underlying platform (android, ios, or web) on which Flutter is running. So, if this MethodChannel is called without options, some coreNotInitialized() is thrown.

The following is from here

// If there is no native default app and the user didn't provide options to
// create one, throw.
if (defaultApp == null && _options == null) {
  throw coreNotInitialized();
}

This snippet is from initializeApp method in method_channel_firebase.dart file.

There is a conditional block two steps before this one that checks if the current platform is android and if there is a default firebase app available from native android resources. This probably explains why options (as well as name) are optional at the high-level exposed package.

Thinking deeper, it makes sense that you must provide FirebaseOptions to Firebase.initializeApp(). If you were coding for a specific platform and you were to integrate Firebase or use any of its services, you have to provide those options in one way or the other. In fact, with Flutter, in the past, when the flutterfire command was not yet available, setting up Firebase for Flutter had to be done manually for each platform (in its own folder).

Now that Dart-only Firebase setup is available in the main.dart file directly, it still requires that the FirebaseOptions for each platform is provided. And that is what the flutterfire CLI does during the setup process. It fetches the configs for each platform and imports them. In fact, the docs also specify that you should run flutterfire configure after adding a specific Firebase product plugin.

READ  [FIXED] android - Align Right LinearLayout to right and fill left LinearLayout
Powered by Inline Related Posts

Solution

So it is clear that to work with any client, you must properly connect a Firebase project from the Firebase console, because the client needs the project’s config or FirebaseOptions.

Since you want to work with demo stuff, use the “explore a demo project” feature in the firebase console. This creates a valid temporary Firebase project.

Then when you run the flutterfire configure command, you select this demo project and use it instead.

Furthermore, the Firebase emulators don’t use production resources. So as you start the emulators with --only firestore, firestore calls will all be done locally. That said, if you configure FlutterFire with a valid Firebase project, and you use the firestore emulator, you have nothing to fear with regards to interacting with production APIs.

Note that you can use the console demo project with whatever emulator for Firebase service. But remember that these services like firestore for example, are not enabled in the demo project in itself, so if you try to test code against production firestore for the demo project, it won’t work.

Also, the demo project in the Firebase console has to be in view while you work with it. There is an “Exit Project” button up top. Once you exist, you’ve lost the demo project and may have to click on “explore a demo project” again from the home screen to access another demo project

Recap

So ensure your main method looks like this:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  FirebaseFirestore.instance.useFirestoreEmulator('localhost', 8080);
  runApp(const MyApp());
}

And even if you are using a demo emulator, it should now work as expected.

Solution 4 :

inside main() you can write like this for initilize :

void main() async { 
   WidgetsFlutterBinding.ensureInitialized(); 
   await Firebase.initializeApp();
}

and other thing you have to write :

inside android/app/build.gradle file at the bottom

apply plugin: 'com.google.gms.google-services'

and try this.

[EDIT]

you can check this github post as issue is accepted from flutter as well :
https://github.com/firebase/flutterfire/issues/8898

Problem :

I am using the firebase emulator with demo project, without any actual firebase project resource created. I am doing so with the following command…

firebase emulators:start --project demo-test --only firestore

This successfully starts a Firebase emulator instanceat localhost:8080.

READ  [FIXED] android - Getting IMEI number without runtime permission
Powered by Inline Related Posts

Then I am connecting to the firebase in my flutter app as follows…

await Firebase.initializeApp();
FirebaseFirestore.instance.settings = const Settings(
  host: 'localhost:8080',
  sslEnabled: false,
  persistenceEnabled: false,
);

When initializeApp is run, I get the following error…

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

Comments

Comment posted by emulator configuration

How are you initializing the emulator? Here is a good answer that can help you with the

Comment posted by Andres Fiesco Casasola

One of the solutions in the case provided suggests moving the

Comment posted by Scorb

This is and incorrect answer. A firebase emulator demo project does NOT have an actual firebase project created. That is the definition of a demo project (see link in initial post). So there are literally no project configuration that can be done.

Comment posted by Obumuneme Nwabude

you can’t configure any firebase client without a valid firebase app config.

Comment posted by Obumuneme Nwabude

and yes in the link you just hyperlinked, the keyword is demo firebase project. You can only access demo firebase projects from the Firebase console. That the firebase emulators were configured to ignore any project once they see demo in the project name doesn’t mean that the client can work without a valid firebase project. You have to provide flutter with the

Comment posted by Obumuneme Nwabude

And there is nothing like firebase emulator demo project, the demo project has to be from the firebase console. and also remember that you need to connect to the emulators the proper way, see the code 1st problem mentioned in the answer, and not with the firestore

Comment posted by stackoverflow.com/questions/67781589/…

stackoverflow.com/questions/67781589/…

Android Tags:android, firebase, flutter, google-cloud-platform, ios

Post navigation

Previous Post: [FIXED] android.media.ImageWriter produces green screen screen on every second posted Image frame
Next Post: [FIXED] android – Notifee native module not found

Related Posts

[FIXED] unity3d – How do you map to KeyCode.JoystickButton0 in the new Unity Input System? Android
[FIXED] Android WorkManager fork single into multiple chains and join Android
[FIXED] android – Custom Notification RemoteView dismiss on swipe Left or Right Android
[FIXED] android – How can i add my notification details to a recycler that is found in another fragment? Android
[FIXED] c# – Is there a way to validate an empty control picker (that receives an integer value) in Xamarin forms? Android
[FIXED] xamarin.forms – How to obfuscate a xamarin App on VS 2019 Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme