未处理的异常:[core/未初始化]Firebase未正确初始化



我的代码显示错误Firebase没有正确初始化

**Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`.
E/flutter (25616): 
E/flutter (25616): View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization
E/flutter (25616):     
E/flutter (25616): #0      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:99:9)
E/flutter (25616): <asynchronous suspension>
E/flutter (25616): #1      Firebase.initializeApp (package:firebase_core/src/firebase.dart:42:31)
E/flutter (25616): <asynchronous suspension>
E/flutter (25616): #2      main (package:a_store_online_shop/main.dart:11:3)
E/flutter (
25616): <asynchronous suspension>
E/flutter (25616): 

这是我们的main.dart文件,出现错误代码firebase没有正确初始化。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。?

main.dart

import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:a_store_online_shop/Screens/main_screen.dart';
import 'package:a_store_online_shop/Screens/on_boarding_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_storage/get_storage.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await GetStorage.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
// title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SplashScreen(),
initialRoute: SplashScreen.id,
routes: {
SplashScreen.id: (context) => const SplashScreen(),
OnBoardingScreen.id: (context) => const OnBoardingScreen(),
},
);
}
}
class SplashScreen extends StatefulWidget {
const SplashScreen({Key? key}) : super(key: key);
static const String id = "splash-screen";
@override
State<SplashScreen> createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
final store = GetStorage();
@override
void initState() {
Timer(const Duration(seconds: 3), () {
bool? _boarding = store.read('onBoarding');
_boarding == null
? Navigator.pushReplacementNamed(context, OnBoardingScreen.id)
: _boarding == true
? Navigator.pushReplacementNamed(context, MainScreen.id)
: Navigator.pushReplacementNamed(context, OnBoardingScreen.id);
});
super.initState();
}
@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge, overlays: []);
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Image.asset("assets/images/logo.jpg"),
),
);
}
}

因此,对于其他所有出现此错误的人,我自己解决了,对于IOS,行

Firebase.initializeApp()

它需要一个选项:

Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)

为此,请遵循此链接上的说明:

https://firebase.flutter.dev/docs/cli

从设备中卸载应用程序并重新安装

Unints从设备上卸载你的应用程序并重新安装,它对我很有效。

最新更新