Home > Software engineering >  How to use a Firebase emulator demo project in Flutter?
How to use a Firebase emulator demo project in Flutter?

Time:11-22

I have a Flutter project where I use Firebase Authentication. I'd like to test it locally. Here is how Firebase local emulator is started:

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

Here is Firebase initialization from main.dart:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
      options: FirebaseOptions(
    apiKey: 'any',
    appId: 'any',
    messagingSenderId: 'any',
    projectId: 'demo-test',
  ));

  //...

  runApp(...);
}

Everything starts smoothly without errors. The app works. However when I try to sign up a new user:

firebase_auth.FirebaseAuth.createUserWithEmailAndPassword(email: email, password: password);

I receive an error:

[firebase_auth/unknown] com.google.firebase.FirebaseException: An internal error has occurred. [ API key not valid. Please pass a valid API key. ]

I suspect that I need to provide a proper API key for demo-test project but where can I find it? Or may be I can provide one when starting the emulator? I couldn't find answer in Google docs.

To clarify things. The app works fine when I use options of my real Firebase project. The problem comes only with a so-called Demo project.

CodePudding user response:

Make sure to call useAuthEmulator in your code before using the authentication service. The Flutter code for that may be missing from the docs (I just filed an issue to get it added there), but the API to call can be found here.

I'm don't exactly recall how I addressed that cleartext issue last time I encountered it, but am quite sure it was by following some top search results including the android:usesCleartextTraffic="true" that you mention.

  • Related