我有一个适用于Android, iOS和web的Flutter应用程序。我在main中这样使用Firebase:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
try {
await Firebase.initializeApp(
// name: 'name',
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(
MultiProvider(
providers: [
// providers
],
child: const MyApp(),
),
);
} catch (e) {
print(e.toString());
}
}
设置name
参数时,Android和iOS都能运行,web不能运行。窗口已创建,但保持白色,并且控制台显示:
[core/no-options] Firebase: Need to provide options, when not being deployed to hosting via source..
当我注释相同的参数时,它在web中运行,但我在Android和iOS中有同样的问题。它说:
flutter: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists
我怎样才能让它同时工作?
您可以使用kIsWeb
检查平台是否为web
,仅当不是web
时才设置name
if(kIsWeb){
await Firebase.initializeApp(
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
);
}else{
await Firebase.initializeApp(
name: 'name',
options: DefaultFirebaseOptions.currentPlatform,
);
}