5

Error : no firebase app has been created call firebase.initializeapp My Question : Where should i need to add firebase initialization

A stateless widget with firestore reference 'users'

class FeedBack extends StatelessWidget {
  CollectionReference users = FirebaseFirestore.instance.collection('users');
  late String txtnote;
  late String note;
  @override
  Widget build(BuildContext context) {
    return Scaffold(

onpress integration for writing data to firestore

child: ElevatedButton(
                    onPressed: () async {
                      await users.add({
                        'subject': note,
                        'email': 'example@gmail.com',
                        'description': txtnote
                      }).then((value) => print('Data Added Successfully!'));
                    },
                    child: Text(
                      'Submit',
                      style: TextStyle(color: Colors.white),
                    ),

Note : This dart file 'feedback.dart' does not contain void main function its a stateless widget

NIKSON PJ
  • 61
  • 1
  • 3
  • 1
    Does this answer your question? [No Firebase App '\[DEFAULT\]' has been created - call Firebase.initializeApp() in Flutter and Firebase](https://stackoverflow.com/questions/63492211/no-firebase-app-default-has-been-created-call-firebase-initializeapp-in) – Ivo Sep 21 '21 at 17:25

2 Answers2

6

You can call inside the main entry point of your app:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 17:58
2

As a default, this should go in the main.dart file

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
adamcapjones
  • 185
  • 1
  • 17
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 21 '21 at 17:58