-1

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 the WidgetsFlutterBinding.ensureInitialized() first. If you're running a test, you can call the TestWidgetsFlutterBinding.ensureInitialized() as the first line in your test's main() method to initialize the binding.)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Your code is querying Cloud Firestore. It doesn't have "buckets". Perhaps you are confusing it with Cloud Storage, which does. – Doug Stevenson Aug 20 '20 at 00:51

2 Answers2

1

try this

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

ref: https://github.com/TheAlphamerc/flutter_twitter_clone/blob/master/lib/main.dart

CH Wing
  • 1,062
  • 1
  • 12
  • 21
1

You need to run WidgetsFlutterBinding.ensureInitialized(); before calling Firebase.initializeApp(); in main()

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
JideGuru
  • 7,102
  • 6
  • 26
  • 48