[英]No Firebase App has been created - call Firebase.initializeApp()
I am trying to run my app and it is giving the following error我正在尝试运行我的应用程序,但出现以下错误
[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() [core/no-app] 没有创建 Firebase App '[DEFAULT]' - 调用 Firebase.initializeApp()
I have already called firebase.initializeApp()
but still the error is same.我已经调用firebase.initializeApp()
但错误仍然相同。 here is my code of main file这是我的主文件代码
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => AppointmentsProvider(),
child: MaterialApp(
title: 'Car Wash App',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: FutureBuilder(
future: Firebase.initializeApp(),
builder: (ctx, snap) =>
snap.connectionState == ConnectionState.waiting
? Center(
child: Text('Loading...'),
)
: StreamBuilder(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (ctx, snapShot) =>
snapShot.hasData ? HomePage() : UserAuth(),
),
),
routes: {
HomePage.routeName: (_) => HomePage(),
HistoryPage.routeName: (_) => HistoryPage(),
MyAppointments.routeName: (_) => MyAppointments(),
PackagesPage.routeName: (_) => PackagesPage(),
VehicleType.routeName: (_) => VehicleType(),
SlotPage.routeName: (_) => SlotPage(),
ExtraServicesPage.routeName: (_) => ExtraServicesPage(),
SummaryPage.routeName: (_) => SummaryPage(),
DetailedScreen.routeName: (_) => DetailedScreen(),
PaymentMethodScreen.routeName: (_) => PaymentMethodScreen(),
},
),
);
}
}
Any kind of help is much appreciated.非常感谢任何形式的帮助。 Thank you谢谢
You should initialise it in main().您应该在 main() 中对其进行初始化。
For example:例如:
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}
If the above answers don't help, you can solve the problem by following steps:如果上述答案没有帮助,您可以通过以下步骤解决问题:
delete the project and take backup of lib folder and pubspec.yaml file.
Create a new project with the same name
copy backed up files to the newly created project.
run flutter pub get
you are good to go
It literally solved my problem.它从字面上解决了我的问题。 I hope this will help.我希望这将有所帮助。
Firebase.initializeApp() is an asynchronous function. Firebase.initializeApp() 是一个异步 function。 You should use "await" to ensure Firebase initialization.您应该使用“等待”来确保 Firebase 初始化。 And use WidgetsFlutterBinding.ensureInitialized() to prevent flutter errors.并使用 WidgetsFlutterBinding.ensureInitialized() 来防止 flutter 错误。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
Future<void> main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(const MyApp()); }
This error is know to occur when you fail to add the required dependencies in both your android side of things in project and app level build.gradle files当您未能在项目和应用程序级别 build.gradle 文件中的 android 方面添加所需的依赖项时,就会发生此错误
Check your android/build.gradle file.检查你的 android/build.gradle 文件。 Inside dependecies add内部依赖添加
classpath 'com.google.gms:google-services:4.3.14' (use latest version)类路径 'com.google.gms:google-services:4.3.14'(使用最新版本)
Go to app/build.gradle file. Go 到 app/build.gradle 文件。 Add the plugin outside dependencies添加插件外部依赖
apply plugin: 'com.google.gms.google-services'应用插件:'com.google.gms.google-services'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.