我正在尝试运行我的应用程序,但出现以下错误
[core/no-app] 未创建 Firebase 应用程序“[DEFAULT]” - 调用 Firebase.initializeApp()
我已经打电话给
firebase.initializeApp()
但错误还是一样。
这是我的主文件代码
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(),
},
),
);
}
}
非常感谢任何形式的帮助。谢谢你
你应该在main()中初始化它。
例如:
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(MyApp());
}
此链接可能对您有帮助。
编辑:第一个链接已存档。 这是在 Flutter 项目中设置 firebase 的新方法。步骤都差不多。最后一个链接包含配置 Firebase 插件的另一个步骤。
步骤是:
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
它确实解决了我的问题。我希望这会有所帮助。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
classpath 'com.google.gms:google-services:4.3.14'(使用最新版本)
应用插件:'com.google.gms.google-services'