flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

🐛 cloud_firestore - Throws 'no default app' exception after Firebase has been initialized with a specific firebase project.

Open Turburlar opened this issue 1 year ago • 0 comments

I am initializing Firebase with a specific project. I am then passing the resulting FirebaseApp to FirebaseFirestore.instanceOf method.

Then when I try to write to a collection, I get the following exception...

FirebaseException ([core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp())

Here is a minimal reproducible example, with a comment indicating the the line that throws the exception...

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    _asyncInit();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Test App"),
      ),
      body: Container(),
    );
  }

  Future<void> _asyncInit() async {
    var app = await Firebase.initializeApp(
      name: "my-throwaway-project",
      options: const FirebaseOptions(
        apiKey: "AIzaSyBXOxFf_iXaer4tX-sE8eRrh77VftTKZI0",
        appId: "1:980810897373:android:b1a4bcd50cd76712868834",
        messagingSenderId: "980810897311",
        projectId: "my-throwaway-project",
      ),
    );

    var db = FirebaseFirestore.instanceFor(app: app);
    await db.collection("test1").add({"field1":"value1"}); // this line throws the exception
  }
}

Turburlar avatar Aug 09 '22 21:08 Turburlar

@Turburlar Looking at the code you shared above, you don't seem to be initializing Firebase before using its service, and hence it is throwing the error and indicating you to call Firebase.initializeApp(). So, you need to update your code as below inside main() and try again:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
runApp(MyApp());

}

var app = await Firebase.initializeApp( name: "my-throwaway-project", options: const FirebaseOptions( apiKey: "AIzaSyBXOxFf_iXaer4tX-sE8eRrh77VftTKZI0", appId: "1:980810897373:android:b1a4bcd50cd76712868834", messagingSenderId: "980810897311", projectId: "my-throwaway-project", ), );

Also, I suggest you to make use of flutterfire configure that writes native config files to your project and you don't have to initialize like you did above. You can simply do:

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

You can read more about it here.

Closing for now, as the error you are getting is due to code implementation and configuration rather than an issue in plugin. If you disagree, write in comments and I'll reopen it.

darshankawar avatar Aug 10 '22 10:08 darshankawar

EDIT: At first it was not clear to me what @darshankawar was indicating, because I was already calling initializeApp.

But to rectify the issue you must ALWAYS call initializeApp() with empty parameters, then you must subsequently call initializeApp(..) with a secondary project parameters.

Turburlar avatar Aug 10 '22 13:08 Turburlar