imane imane
imane imane

Reputation: 159

Error: Assertion failed: file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-2.0.1/lib/src/firebase_core_web.dart

I'm a beginner in flutter, i want to use firebase in my flutter app, i run my app on chrome because the emulator does not work, i have configured the build.gradle file, and the pubspec.yaml, when i run my app i have an error that i don't understand saying:

Error: Assertion failed: file:///C:/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-2.0.1/lib/src/firebase_core_web.dart:207:11

My code is :

await Firebase.initializeApp();
runApp(MyApp());
createUser(name: 'imane');
}

Future createUser({required String name}) async {
final docUser = FirebaseFirestore.instance
    .collection('users')
    .doc('PyRPEmg2tjbcoqLf0pEH');
final json = {
  'name': name,
  'age': 21,
  'birthday': DateTime(2001, 7, 28),
};
await docUser.set(json);
}

Upvotes: 0

Views: 2063

Answers (1)

Flo Art
Flo Art

Reputation: 41

Error: Assertion failed: file:///C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-2.0.1/lib/src/firebase_core_web.dart:207:11

There is a solution:

1) In the library folder, create a folder and a dart file: lib/shared/constants.dart

In the constants.dart file, paste the code below:

class Constants {
  static String appId = " ";
  static String apiKey = " ";
  static String messagingSenderId = " ";
  static String projectId = " ";
}

Next, you'll copy your data from: Firebase - Project settings - Web apps - SDK setup and configuration, to constants.dart file.

2) To main.dart at the very top paste the code below:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if(kIsWeb){
    await Firebase.initializeApp(
        options: FirebaseOptions(
            apiKey: Constants.apiKey,
            appId: Constants.appId,
            messagingSenderId: Constants.messagingSenderId,
            projectId: Constants.projectId));
  }

  else{
    await Firebase.initializeApp();
  }
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
   const MyApp({Key? key}) : super(key: key);

do the import:

import 'package:flutter/foundation.dart';
import 'package:shop_app_admin/shared/constants.dart';

Upvotes: 4

Related Questions