xxxxxxxxxx
//It means you have not initialized your app.
//Maybe the location of the firebase_config file is written wrong.
//Else you can initialise your app by this script:
var firebaseConfig = {
apiKey: " <Provided by Firebase> ",
authDomain: " <Provided by Firebase> ",
projectId: " <Provided by Firebase> ",
storageBucket: " <Provided by Firebase> ",
databaseURL: " <Provided by Firebase> ",
messagingSenderId: " <Provided by Firebase> ",
appId: " <Provided by Firebase> ",
measurementId: " <Provided by Firebase> "
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
var storage = firebase.storage();
var storageRef = firebase.storage().ref();
xxxxxxxxxx
import 'package:firebase_core/firebase_core.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
xxxxxxxxxx
You need to Firebase.initializeApp() before you can access database. You can initialize firebase in main void.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
xxxxxxxxxx
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
var fsconnect = FirebaseFirestore.instance;
myget() async {
var d = await fsconnect.collection("students").get();
// print(d.docs[0].data());
for (var i in d.docs) {
print(i.data());
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Firebase Firestore App'),
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text('send data'),
onPressed: () {
fsconnect.collection("students").add({
'name': 'sarah',
'title': 'xyz',
'email': 'sarah@gmail.com',
});
print("send ..");
},
),
RaisedButton(
child: Text('get data'),
onPressed: () {
myget();
print("get data ...");
},
)
],
),
));
}
}
xxxxxxxxxx
Since August 2017, all Firebase services have been updated so that
you have to call Firebase.initializeApp() in your main before
you can use any of the Firebase products, for example:
If you want to use the firebase_core plugin in a flutter application,
then in your pubspec.yaml file add the firebase_core as below
dependencies:
flutter:
sdk: flutter
firebase_core : ^1.4.0
Then call the Firebase.initializeApp(), in your main.dart file's
main function:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
xxxxxxxxxx
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
var fsconnect = FirebaseFirestore.instance;
myget() async {
var d = await fsconnect.collection("students").get();
// print(d.docs[0].data());
for (var i in d.docs) {
print(i.data());
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Firebase Firestore App'),
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text('send data'),
onPressed: () {
fsconnect.collection("students").add({
'name': 'sarah',
'title': 'xyz',
'email': 'sarah@gmail.com',
});
print("send ..");
},
),
RaisedButton(
child: Text('get data'),
onPressed: () {
myget();
print("get data ...");
},
)
],
),
));
}
}
xxxxxxxxxx
@override
void initState() {
super.initState();
Firebase.initializeApp().whenComplete(() {
print("completed");
setState(() {});
});
}