Home > Back-end >  Where to add Firebase.initializeApp in flutter? [duplicate]
Where to add Firebase.initializeApp in flutter? [duplicate]

Time:09-22

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': '[email protected]',
                        '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

CodePudding user response:

You can call inside de main entry point of your App:

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

CodePudding user response:

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
  • Related