flutter 初始化firebase失败

q35jwt9p  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(161)

当我运行我的应用程序时,我在日志中得到此错误
[🌎Easy Localization] [WARNING]本地化密钥[无法初始化firebase![未找到
但关键是在我的资产中/在en.json中翻译为"Failed to initialise firebase!": "Failed to initialise firebase!",,在ar.json中翻译为"Failed to initialise firebase!": "فشل تهيئة Firebase!",
当我运行我的应用程序时,我在屏幕上看到错误Failed to initialise firebase!
这是我的代码

await UserPreference.init();
runApp(
  MultiProvider(
    providers: [
      Provider<CartDatabase>(
        create: (_) => CartDatabase(),
      )
    ],
    child: EasyLocalization(
        supportedLocales: [Locale('en'), Locale('ar')],
        path: 'assets/translations',
        fallbackLocale: Locale('en'),
        saveLocale: true,
        useOnlyLangCode: true,
        useFallbackTranslations: true,
        child: MyApp()),
  ),
);

字符串
这也是显示错误Failed to initialise firebase! screenshot的代码的另一部分

Show error message if initialization failed
if (_error) {
  return MaterialApp(
      locale: context.locale,
      supportedLocales: context.supportedLocales,
      localizationsDelegates: context.localizationDelegates,
      home: Scaffold(
        body: Container(
          color: Colors.white,
          child: Center(
              child: Column(
            children: [
              Icon(
                Icons.error_outline,
                color: Colors.red,
                size: 25,
              ),
              SizedBox(height: 16),
              Text(
                'Failed to initialise firebase!'.tr(),
                style: TextStyle(color: Colors.red, fontSize: 25),
              ),
            ],
          )),
        ),
      ));
}


我试着检查我写的密钥是否与我的main中的密钥完全相同。dart要解决这个问题,请确保本地化密钥“Failed to initialize firebase!“存在于我的应用程序中所有支持语言的本地化文件中
i已确保该键拼写正确,并且与警告消息中使用的大小写匹配(因为本地化键区分大小写),

3yhwsihp

3yhwsihp1#

确保您已将翻译文件添加到pubspec.yaml

flutter:
  assets:
    - assets/translates/

字符串
然后等待初始化EasyLocalization,如下所示:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();

  // ...
}


通常也会将键写为“lower_case_and_no_space”,所以我建议更改它。如果您使用json文件进行翻译,我还建议您使用EasyLocalization包提供的代码生成。您需要在终端中运行flutter pub run easy_localization:generate -S "assets/translates" -O "lib/translations" -f keys -o translations.g.dart,然后您可以更轻松地使用密钥:
en.json:

{
  "firebase_error": "Failed to initialise firebase!"
}


在你的app中:

import 'translations/translations.g.dart';

// ...
Text(
  LocaleKeys.firebase_error.tr(),
)


更多信息请看这里:https://pub.dev/packages/easy_localization

相关问题