i'm building an epressing app with Flutter. And now after initializing firebase in the main, nothing appears when i run (just a empty blank page). But the run and everythings were alright befor using firebase. I really need help. This is my code :
In the run i got this :
Running with unsound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
Debug service listening on ws://127.0.0.1:59100/Wdv6NyVJqKw=/ws
Flutter Web Bootstrap: Auto
Error: Assertion failed: file:///C:/Users/Asus/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.7.1/lib/src/firebase_core_web.dart:207:11
options != null
"FirebaseOptions cannot be null when creating the default app."
at Object.throw_ [as throw] (http://localhost:58760/dart_sdk.js:5405:11)
at Object.assertFailed (http://localhost:58760/dart_sdk.js:5327:15)
at firebase_core_web.FirebaseCoreWeb.new.initializeApp (http://localhost:58760/packages/firebase_core_web/firebase_core_web.dart.lib.js:241:42)
at initializeApp.next (<anonymous>)
at http://localhost:58760/dart_sdk.js:43063:33
at _RootZone.runUnary (http://localhost:58760/dart_sdk.js:42919:58)
at _FutureListener.thenAwait.handleValue (http://localhost:58760/dart_sdk.js:37493:29)
at handleValueCallback (http://localhost:58760/dart_sdk.js:38088:49)
at _Future._propagateToListeners (http://localhost:58760/dart_sdk.js:38126:17)
at [_completeWithValue] (http://localhost:58760/dart_sdk.js:37955:23)
at http://localhost:58760/dart_sdk.js:37112:46
at _RootZone.runUnary (http://localhost:58760/dart_sdk.js:42919:58)
at _FutureListener.then.handleValue (http://localhost:58760/dart_sdk.js:37493:29)
at handleValueCallback (http://localhost:58760/dart_sdk.js:38088:49)
at _Future._propagateToListeners (http://localhost:58760/dart_sdk.js:38126:17)
at [_completeWithValue] (http://localhost:58760/dart_sdk.js:37955:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:58760/dart_sdk.js:37991:35)
at Object._microtaskLoop (http://localhost:58760/dart_sdk.js:43223:13)
at _startMicrotaskLoop (http://localhost:58760/dart_sdk.js:43229:13)
at http://localhost:58760/dart_sdk.js:38359:9
in the "file:///C:/Users/Asus/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core_web-1.7.1/lib/src/firebase_core_web.dart:207:11", from line 144 to 209 i got this :
@override
Future<FirebaseAppPlatform> initializeApp({
String? name,
FirebaseOptions? options,
}) async {
await _initializeCore();
guardNotInitialized(() => firebase.SDK_VERSION);
assert(
() {
if (firebase.SDK_VERSION != supportedFirebaseJsSdkVersion) {
// ignore: avoid_print
print(
'''
WARNING: FlutterFire for Web is explicitly tested against Firebase JS SDK version "$supportedFirebaseJsSdkVersion"
but your currently specifying "${firebase.SDK_VERSION}" by either the imported Firebase JS SDKs in your web/index.html
file or by providing an override - this may lead to unexpected issues in your application. It is recommended that you change all of the versions of the
Firebase JS SDK version "$supportedFirebaseJsSdkVersion":
If you override the version manually:
change:
<script>window.flutterfire_web_sdk_version = '${firebase.SDK_VERSION}';</script>
to:
<script>window.flutterfire_web_sdk_version = '$supportedFirebaseJsSdkVersion';</script>
If you import the Firebase scripts in index.html, instead allow FlutterFire to manage this for you by removing
any Firebase scripts in your web/index.html file:
e.g. remove: <script src="https://www.gstatic.com/firebasejs/${firebase.SDK_VERSION}/firebase-app.js"></script>
''',
);
}
return true;
}(),
);
firebase.App? app;
if (name == null || name == defaultFirebaseAppName) {
bool defaultAppExists = false;
try {
app = firebase.app();
defaultAppExists = true;
} catch (e) {
// noop
}
if (defaultAppExists) {
if (options != null) {
// If there is a default app already and the user provided options do a soft
// check to see if options are roughly identical (so we don't unnecessarily
// throw on minor differences such as platform specific keys missing,
// e.g. hot reloads/restarts).
if (options.apiKey != app!.options.apiKey ||
options.databaseURL != app.options.databaseURL ||
options.storageBucket != app.options.storageBucket) {
// Options are different; throw.
throw duplicateApp(defaultFirebaseAppName);
}
}
} else {
assert(
options != null,
'FirebaseOptions cannot be null when creating the default app.',
);
The main.dart code :
// @dart=2.9
//les importations : library & others pages
import 'package:flutter/material.dart';
import 'onbroding_page.dart';
import 'package:firebase_core/firebase_core.dart';
//le void main
Future<void> main() async{
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
//Stateless du MaterialApp
class MyApp extends StatelessWidget {
MyApp({Key key}) : super(key: key);
static final String title = "Eclat d'Afrik";
@override
Widget build(BuildContext context) => MaterialApp(
debugShowCheckedModeBanner: false,
title: title,
home: OnBrodingPage(),
);
}
The whole firebase_core_web.dart code :
// ignore_for_file: unsafe_html
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of firebase_core_web;
/// Defines a Firebase service by name.
class FirebaseWebService {
/// The name which matches the Firebase JS Web SDK postfix.
String name;
/// Naming of Firebase web products is different from Flutterfire plugins. This
/// property allows overriding of web naming to Flutterfire plugin naming.
String? override;
/// Creates a new [FirebaseWebService].
FirebaseWebService._(this.name, [this.override]);
}
/// The entry point for accessing Firebase.
///
/// You can get an instance by calling [FirebaseCore.instance].
class FirebaseCoreWeb extends FirebasePlatform {
static Map<String, FirebaseWebService> _services = {
'core': FirebaseWebService._('app', 'core'),
'app-check': FirebaseWebService._('app-check', 'app_check'),
'remote-config': FirebaseWebService._('remote-config', 'remote_config'),
};
/// Internally registers a Firebase Service to be initialized.
static void registerService(String service) {
_services.putIfAbsent(service, () => FirebaseWebService._(service));
}
/// Registers that [FirebaseCoreWeb] is the platform implementation.
static void registerWith(Registrar registrar) {
FirebasePlatform.instance = FirebaseCoreWeb();
}
/// Returns the Firebase JS SDK Version to use.
///
/// You can override the supported version by attaching a version string to
/// the window (window.flutterfire_web_sdk_version = 'x.x.x'). Do so at your
/// own risk as the version might be unsupported or untested against.
String get _firebaseSDKVersion {
return context['flutterfire_web_sdk_version'] ??
supportedFirebaseJsSdkVersion;
}
/// Returns a list of services which won't be automatically injected on
/// initilization. This is useful incases where you wish to manually include
/// the scripts (e.g. in countries where you must request the users permission
/// to include Analytics).
///
/// You can do this by attaching an array of services to the window, e.g:
///
/// window.flutterfire_ignore_scripts = ['analytics'];
///
/// You must ensure the Firebase script is injected before using the service.
List<String> get _ignoredServiceScripts {
try {
JsObject ignored =
JsObject.fromBrowserObject(context['flutterfire_ignore_scripts']);
if (ignored is Iterable) {
return (ignored as Iterable)
.map((e) => e.toString())
.toList(growable: false);
}
} catch (e) {
// Noop
}
return [];
}
/// Injects a `script` with a `src` dynamically into the head of the current
/// document.
Future<void> _injectSrcScript(String src, String windowVar) async {
ScriptElement script = ScriptElement();
script.type = 'text/javascript';
script.crossOrigin = 'anonymous';
script.text = '''
window.ff_trigger_$windowVar = async (callback) => {
callback(await import("$src"));
};
''';
assert(document.head != null);
document.head!.append(script);
Completer completer = Completer();
context.callMethod('ff_trigger_$windowVar', [
(module) {
context[windowVar] = module;
context.deleteProperty('ff_trigger_$windowVar');
completer.complete();
}
]);
await completer.future;
}
/// Initializes the Firebase JS SDKs by injecting them into the `head` of the
/// document when Firebase is initialized.
Future<void> _initializeCore() async {
// If Firebase is already available, core has already been initialized
// (or the user has added the scripts to their html file).
if (context['firebase_core'] != null) {
return;
}
String version = _firebaseSDKVersion;
List<String> ignored = _ignoredServiceScripts;
await Future.wait(
_services.values.map((service) {
if (ignored.contains(service.override ?? service.name)) {
return Future.value();
}
return _injectSrcScript(
'https://www.gstatic.com/firebasejs/$version/firebase-${service.name}.js',
'firebase_${service.override ?? service.name}',
);
}),
);
}
/// Returns all created [FirebaseAppPlatform] instances.
@override
List<FirebaseAppPlatform> get apps {
return guardNotInitialized(
() => firebase.apps.map(_createFromJsApp).toList(growable: false),
);
}
/// Initializes a new [FirebaseAppPlatform] instance by [name] and [options] and returns
/// the created app. This method should be called before any usage of FlutterFire plugins.
///
/// The default app instance cannot be initialized here and should be created
/// using the platform Firebase integration.
@override
Future<FirebaseAppPlatform> initializeApp({
String? name,
FirebaseOptions? options,
}) async {
await _initializeCore();
guardNotInitialized(() => firebase.SDK_VERSION);
assert(
() {
if (firebase.SDK_VERSION != supportedFirebaseJsSdkVersion) {
// ignore: avoid_print
print(
'''
WARNING: FlutterFire for Web is explicitly tested against Firebase JS SDK version "$supportedFirebaseJsSdkVersion"
but your currently specifying "${firebase.SDK_VERSION}" by either the imported Firebase JS SDKs in your web/index.html
file or by providing an override - this may lead to unexpected issues in your application. It is recommended that you change all of the versions of the
Firebase JS SDK version "$supportedFirebaseJsSdkVersion":
If you override the version manually:
change:
<script>window.flutterfire_web_sdk_version = '${firebase.SDK_VERSION}';</script>
to:
<script>window.flutterfire_web_sdk_version = '$supportedFirebaseJsSdkVersion';</script>
If you import the Firebase scripts in index.html, instead allow FlutterFire to manage this for you by removing
any Firebase scripts in your web/index.html file:
e.g. remove: <script src="https://www.gstatic.com/firebasejs/${firebase.SDK_VERSION}/firebase-app.js"></script>
''',
);
}
return true;
}(),
);
firebase.App? app;
if (name == null || name == defaultFirebaseAppName) {
bool defaultAppExists = false;
try {
app = firebase.app();
defaultAppExists = true;
} catch (e) {
// noop
}
if (defaultAppExists) {
if (options != null) {
// If there is a default app already and the user provided options do a soft
// check to see if options are roughly identical (so we don't unnecessarily
// throw on minor differences such as platform specific keys missing,
// e.g. hot reloads/restarts).
if (options.apiKey != app!.options.apiKey ||
options.databaseURL != app.options.databaseURL ||
options.storageBucket != app.options.storageBucket) {
// Options are different; throw.
throw duplicateApp(defaultFirebaseAppName);
}
}
} else {
assert(
options != null,
'FirebaseOptions cannot be null when creating the default app.',
);
// At this point, there is no default app so we need to create it with
// the users options.
app = firebase.initializeApp(
apiKey: options!.apiKey,
authDomain: options.authDomain,
databaseURL: options.databaseURL,
projectId: options.projectId,
storageBucket: options.storageBucket,
messagingSenderId: options.messagingSenderId,
appId: options.appId,
measurementId: options.measurementId,
);
}
}
// Ensure the user has provided options for secondary apps.
if (name != null && name != defaultFirebaseAppName) {
assert(
options != null,
'FirebaseOptions cannot be null when creating a secondary Firebase app.',
);
try {
app = firebase.initializeApp(
name: name,
apiKey: options!.apiKey,
authDomain: options.authDomain,
databaseURL: options.databaseURL,
projectId: options.projectId,
storageBucket: options.storageBucket,
messagingSenderId: options.messagingSenderId,
appId: options.appId,
measurementId: options.measurementId,
);
} catch (e) {
if (_getJSErrorCode(e) == 'app/duplicate-app') {
throw duplicateApp(name);
}
throw _catchJSError(e);
}
}
return _createFromJsApp(app!);
}
/// Returns a [FirebaseAppPlatform] instance.
///
/// If no name is provided, the default app instance is returned.
/// Throws if the app does not exist.
@override
FirebaseAppPlatform app([String name = defaultFirebaseAppName]) {
firebase.App app;
try {
app = guardNotInitialized(() => firebase.app(name));
} catch (e) {
if (_getJSErrorCode(e) == 'app/no-app') {
throw noAppExists(name);
}
throw _catchJSError(e);
}
return _createFromJsApp(app);
}
}
/// Converts a Exception to a FirebaseAdminException.
Never _handleException(Object exception, StackTrace stackTrace) {
if (exception.toString().contains('of undefined')) {
throw coreNotInitialized();
}
Error.throwWithStackTrace(exception, stackTrace);
}
/// A generic guard wrapper for API calls to handle exceptions.
R guardNotInitialized<R>(R Function() cb) {
try {
final value = cb();
if (value is Future) {
return value.catchError(
_handleException,
) as R;
}
return value;
} catch (error, stackTrace) {
_handleException(error, stackTrace);
}
}
CodePudding user response:
For your firebase_core
version is seems to be sufficient to pass the FirebaseOptions
once you initialize firebase in your flutter code.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
// Replace with actual values
options: FirebaseOptions(
apiKey: "XXX",
appId: "XXX",
messagingSenderId: "XXX",
projectId: "XXX",
),
);
runApp(MyApp());
}