2

I know this question had been asked 100 times before, and yes I have gone over them and I don't know why my Firebase wont initialize. If someone could take a look see and give some insight, its just the base flutter generated app that I'm trying to connect at this time....

Not listed below but things tried are the initialize in setState({}), FutureBuilder(), and remakeing a new FireStore project/db.

google-services.json location:

enter image description here

main:

    import 'package:flutter/material.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:firebase_core/firebase_core.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @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++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }

pubspec

dependencies:
  flutter:
    sdk: flutter
  firebase_core:
  firebase_auth: ^0.18.4+1
  firebase_storage: ^4.0.1
  cupertino_icons: ^1.0.0

build\gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.4'

    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app\build\gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.example.fish_app"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:26.1.1')
    implementation 'com.google.firebase:firebase-analytics'
}
Joshua
  • 31
  • 4

2 Answers2

1

You can create a method where you would initalize firebase and use this method as an future to Future Builder. You can do something like:-

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHome(),
  );}
}
 // ignore if some imports are not present 
class MyHome extends StatelessWidget{
 //initializing firebase in an method and giving it as an future
 
  initializeFirebase() async{
    await Firebase.initializeApp();
  }

  @override
  Widget build(BuildContext context){
    return FutureBuilder(
      future:initializeFirebase(),
      builder:(context,snapshot){
        if(snapshot.connectionState==ConnectionState.active){
          // return any Widget you like
           return HomePage();//navigate to any page say HomePage
        }
        else{
          return Center(child:Text("Loading..."));
        }
      }
    );
  } 
}
Vinay Jain
  • 398
  • 2
  • 6
  • Yes, i have tried this already. For some reason no matter what i do it says Firebase has not bee correctly initialized. – Joshua Dec 13 '20 at 04:33
1

add apply plugin: 'com.google.gms.google-services' in app\build\gradle:

Your app\build\gradle: should be

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.example.fish_automation_app"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:26.1.1')
    implementation 'com.google.firebase:firebase-analytics'
}

apply plugin: 'com.google.gms.google-services
Jewel Rana
  • 2,397
  • 1
  • 19
  • 28
  • When that is added I get "Execution failed for task ':app:processDebugGoogleServices'.", when i researched that I find [this answer](https://stackoverflow.com/questions/33572465/gradle-errorexecution-failed-for-task-appprocessdebuggoogleservices) which recommends to remove it. When removed the app starts fine but cant initialize firebase – Joshua Dec 13 '20 at 16:15
  • recreate project and try to configure firebase. its seems that it was firebase configuration related problem . – Jewel Rana Dec 14 '20 at 13:00