Sam12
Sam12

Reputation: 1805

core not-initialized Firebase has not been correctly initialized

I currently have a piano app which plays sounds when pressing any button, here's a snippet of the code in main.dart, pubspec.yaml and build.gradle :

I would like to use firebase, but each time I run the code I get the follwing error:

Error: [core/not-initialized] Firebase has not been correctly initialized. Have you added the Firebase import scripts to your index.html file? 

main.dart:

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

class App extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return MaterialApp(
            home: Scaffold(
                body: Center(
                    child: Text(snapshot.error.toString(),
                        textDirection: TextDirection.ltr)
                )
            ),
          );
        }
        if (snapshot.connectionState == ConnectionState.done) {
          return MyApp();
        }
        return Center(child: CircularProgressIndicator());
      },
    );
  }
}


class MyApp extends StatefulWidget {
  @override

  _MyAppState createState() => _MyAppState();

}


class _MyAppState extends State<MyApp> {
  static AudioCache player = AudioCache();

  Widget myTone(Color myColor, int myNumber) {
    return Expanded(
      child: FlatButton(
          color: myColor,
          onPressed: () {
            pressedToneList.add(myNumber);
            print(pressedToneList);
            setState(() {
              player.play('note$myNumber.wav');
            });
          },
          child: null),
    );
  }

  @override
  Widget build(BuildContext context) {
    // Set landscape orientation
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('simon game'),
        ),
        body: SafeArea(
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              myTone(Colors.red, 1),
              myTone(Colors.orange, 2),
              myTone(Colors.yellowAccent, 3),
              myTone(Colors.lightBlueAccent, 4),
              myTone(Colors.purple, 5),
              myTone(Colors.pinkAccent, 6),
              sendButton(7),
            ],
          ),
        ),
      ),
    );
  }
}

pubspec.yaml :

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  audioplayers: ^0.15.1
  firebase_core: ^0.7.0

build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:3.5.0'
    classpath 'com.google.gms:google-services:4.3.3'
}

app/build.gradle:

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.technion.pianogameflutterapp"
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

....

dependencies {
    implementation 'com.android.support:multidex:1.0.3'
    implementation platform('com.google.firebase:firebase-bom:27.1.0')
}

Upvotes: 1

Views: 9772

Answers (3)

RominHood
RominHood

Reputation: 357

For people who get this error on IOS, make sure you copied the GoogleService-info.plist file using XCode.

Simply drag and dropping the file in the finder doesn't work.

Upvotes: 3

Use the Firebase tool installed in Android studio and follow the steps.

After that, reinstall your app in your device. That should work.

Upvotes: 0

Nishuthan S
Nishuthan S

Reputation: 1735

Android apply plugin: 'com.google.gms.google-services' at the end of /android/app/build.gradle and make sure you have places the google-service.json in your app directory /android/app/google-service.json

Web (in index.html)

<script src="https://www.gstatic.com/firebasejs/7.20.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.4.1/firebase-firestore.js"></script>
<script>
  var firebaseConfig = {
    apiKey: "",
    authDomain: "",
    projectId: "",
    storageBucket: "",
    messagingSenderId: "",
    appId: "",
    measurementId: ""
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  firebase.analytics();
</script>

then flutter clean and run your app.

Upvotes: 5

Related Questions