Hello I'm trying to get a list of images from the Firebase Bucket/Storage but I keep getting an error code when I call runApp(). Below is the code and the error message that corresponds to my problem. Any help would me much appreciated. I've tried doing what the error message recommended but I end up with a problem anyways.
import 'package:authorize_user/Screens/image_screen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
void main() {
Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Images',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: ImageScreen(),
);
}
}
class ImageScreen extends StatelessWidget {
Stream<QuerySnapshot> getDocuments() {
return FirebaseFirestore.instance
.collection('Accounts/ZqEbRkLFbW34CXKybmr7/images')
.snapshots();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<QuerySnapshot>(
stream: getDocuments(),
builder: (context, streamSnapshots) {
if (streamSnapshots.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
final documents = streamSnapshots.data.docs;
return ListView.builder(
itemCount: streamSnapshots.data.docs.length,
itemBuilder: (context, index) => Container(
padding: EdgeInsets.all(8),
child: Text(documents[index].data().length.toString()),
),
);
},
),
floatingActionButton:
FloatingActionButton(child:Icon(Icons.add), onPressed: () {}),
);
}
}
Error Message:
Exception has occurred. FlutterError (ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. If you're running an application and need to access the binary messenger before
runApp()
has been called (for example, during plugin initialization), then you need to explicitly call theWidgetsFlutterBinding.ensureInitialized()
first. If you're running a test, you can call theTestWidgetsFlutterBinding.ensureInitialized()
as the first line in your test'smain()
method to initialize the binding.)