1

I started to learn flutter again (started some time ago but stopped). You may find my code below. If I run the app on my smartphone it gives me the error: [core/no-app] No Firebase App['DEFAULT'} has been created - call Firebase.initializeApp(). I read in the documentation, but since I use the recent versions firebase_core: ^0.5.0, firebase_auth: ^0.18.0+1, cloud_firestore: ^0.14.0+2 it seems like the documentation isnt finished or I didnt get it. Where do I need to initialize that one? Why isnt that one enough: CollectionReference users = FirebaseFirestore.instance.collection('users');

timeline.dart (code is mostly from flutter getting started):

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:example/widgets/header.dart';
import 'package:example/widgets/progress.dart';

class Timeline extends StatefulWidget {
  @override
  _TimelineState createState() => _TimelineState();
}

class _TimelineState extends State<Timeline> {
  @override
  void initState() {
    // getUserById();
    super.initState();
  }
   
  @override
  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection('users');
    return StreamBuilder<QuerySnapshot>(
      stream: users.snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return Text('Something went wrong');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return Text("Loading");
        }

        return new ListView(
          children: snapshot.data.documents.map((DocumentSnapshot document) {
            return new ListTile(
              title: new Text(document.data()['username']),
              subtitle: new Text(document.data()['posts_count']),
            );
          }).toList(),
        );
      },
    );
  }
}

This is my code for main.dart

    import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'pages/home.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.red,
        accentColor: Colors.blue,
      ),
      home: Home(),
    );
  }
}

Thank you so much in advance!

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
talaash
  • 55
  • 3
  • 7

2 Answers2

2

In your main.dart, add the following

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); //add this
  await Firebase.initializeApp(); //initialize here
  runApp(MyApp());
}

Visit the new docs to learn more about working with the firebase packages

ByteMe
  • 1,575
  • 1
  • 12
  • 23
  • You are awesome!!! That was the solution. :)) I wasn't sure where to add Firebase.initializeapp(); but now I know. Thank you so much! Much appreciated. – talaash Sep 19 '20 at 17:55
  • You're welcome! Please mark this as the answer to help others with similar questions. – ByteMe Sep 19 '20 at 18:24
1

You can modify your MyApp widget like this, to make things work.

class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: FutureBuilder(
            future: Firebase.initializeApp(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                if (snapshot.hasError) {
                  print(snapshot.error.toString());
                  return Center(child: Text('Error'));
                } else {
                  // ! RETURN THE SCREEN YOU WANT HERE
                  return Timeline();
                }
            },
          ),
        );
      }
    }
SLendeR
  • 839
  • 7
  • 25
  • Thank you SLendeR. :))) I'm sure that your solution would work as well but I have inside home.dart (home()) a page router. Would it work also if I return home() instead of timeline? Because timeline is just another page. – talaash Sep 19 '20 at 17:54
  • Yes it would work, you can use this documentation whenever you use Firesbase with Flutter, it'll help you with a lot of things; https://firebase.flutter.dev/docs/overview/ – SLendeR Sep 20 '20 at 16:05