3

[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

I kinda understand the error but whereever I put it, it doesn't work... I am trying to return only a string and a number value from firebase. And I also did all the requirements. build.graddle in android folder, and build.gradle in android/app folder. Everything. In addition to that I also did: cloud_firestore:

to the dependencies on the pubspec.yaml. Now my main.dart looks like this, but gives so much errors I can't paste here. It's very long. I was using this video as a base but it doesn't work..

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  Widget _buildListItem(BuildContext context, DocumentSnapshot document) {
    return ListTile(
      title: Row(
        children: [
          Expanded(
            child: Text(
              document.data()['adSoyad'],
            ),
          ),
          Container(
            decoration: const BoxDecoration(
              color: Color(0xffddddff),
            ),
            padding: const EdgeInsets.all(10.0),
            child: Text(
              document.data()['yas'].toString(),
              style: Theme.of(context).textTheme.headline4,
            ),
          ),
        ],
      ),
      onTap: () {
        print("bisey");
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection('tablolar').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) return const Text('Loading...');
            return ListView.builder(
              itemExtent: 80.0,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _buildListItem(context, snapshot.data.documents[index]),
            );
          }),
    );
  }
}
Stayheuh
  • 127
  • 1
  • 3
  • 12

1 Answers1

11

inside main function add this two lines and make your function asynchronous. like this

 void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp();
 runApp(MyApp());
 } 

and one more thing, you also need to add firebase_core to your project. otherwise firebase will throw and error

In your pubsec.yaml file add firebase core

  firebase_core: ^0.5.3

and then import it into your main.dart

import 'package:firebase_core/firebase_core.dart';
GloatingCoder
  • 1,124
  • 9
  • 13
  • This works, thanks! But why? I didn't see that in the official docs. That is soo annoying. I find pieces all around the internet. – Stayheuh Jan 05 '21 at 10:06
  • 1
    it is due to the recent firebase package updates, it has to with things like provide current user as a getter when your code is run rather than asynchronously requesting it. you don't even have to initialize it in your main.dart. you can initialize it anywhere from which point your app needs to use firebase. – GloatingCoder Jan 05 '21 at 11:00
  • Yes, this worked in my case for flutter project. – Anil Gupta Jul 20 '22 at 07:13