I'm trying to retrieve data from firestore database. But there's an exception throwing to me saying:
PlatformException
code: 'null-error'
message: 'Host platform returned null value for non-null return value.
Here's my main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(
MaterialApp(
home: const Screen1(),
),
);
}
And here's my screen_1.dart
class Screen1 extends StatefulWidget {
const Screen1({Key? key}) : super(key: key);
@override
State<Screen1> createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> {
List<String> student = [];
Future getStudent() async {
await FirebaseFirestore.instance.collection('student_Info').get().then(
(snapshot) => snapshot.docs.forEach(
(document) {
print(document.reference);
},
),
);
}
@override
void initState() {
getStudent();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
At first I called flutterfire configure
in terminal and select a project. Then I created collection. And also I changed minsdkversion to 21 in \android\app\build.gradle. What's wrong with my code???
Appreciate if someone can advise. Thank you in advance!
CodePudding user response:
If you've configure flutterfire cli well then all you have to do is import and use it in the options like this:
...
import 'firebase_options.dart';
...
void main(){
...
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
}
...
CodePudding user response:
First, you need to run flutterfire configure
in the terminal, it will automatically generate the firebase_options.dart
file. Then you can try the following code:
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(
MaterialApp(
home: const Screen1(),
),
);
}