首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置UnsupportedError时引发的localizationsDelegates

设置UnsupportedError时引发的localizationsDelegates
EN

Stack Overflow用户
提问于 2022-11-14 15:21:34
回答 1查看 30关注 0票数 0

我正在尝试为我的应用程序实现国际化,但它不允许我使用我喜欢的语言(印度尼西亚语)。

从医生那里,它让我把localizationsDelegates添加到我的MaterialApp中。由于我使用的是GetX,这里的MaterialApp被包装在GetMaterialApp中。

但是当我这么做的时候,它给我一个错误:

代码语言:javascript
运行
复制
Exception has occurred.
UnsupportedError (Unsupported operation: Cannot modify unmodifiable map)

我试图删除localizationsDelegates,它抛出了另一个错误:

代码语言:javascript
运行
复制
Exception has occurred.
FlutterError (No MaterialLocalizations found.
TabBar widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
The specific widget that could not find a MaterialLocalizations ancestor was:
  TabBar

但是,当我硬编码GetMaterialAppGetMaterialApp属性到Locale('en', 'US')并注释localizationsDelegates__时,它就能工作了。

你们知道为什么怎么解决吗?

无论如何,这是我的main.dart (和一些相关文件)的样子。

main.dart

代码语言:javascript
运行
复制
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Get.putAsync(() => EnvService().init());
  await Get.putAsync(() => IntlService().init());

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: "MyApp",
      initialRoute: AppPages.INITIAL,
      getPages: AppPages.routes,
      themeMode: ThemeMode.dark,
      theme: MyAppTheme.light,
      darkTheme: MyAppTheme.dark,
      debugShowCheckedModeBanner: false,
      // translations: AppTranslations(),
      locale: const Locale('en', 'US'),
      fallbackLocale: const Locale('id', 'ID'),
      supportedLocales: const [
        Locale('en', 'US'),
        Locale('id', 'ID'),
      ],
      //
      //
      // UNCOMMENT THIS LINE, YOUR PHONE WILL EXPLODE!
      // localizationsDelegates: GlobalMaterialLocalizations.delegates,
    );
  }
}

env_service.dart

代码语言:javascript
运行
复制
class EnvService extends GetxService {
  static EnvService get instance => Get.find();

  Future<EnvService> init() async {
    await dotenv.load();
    return this;
  }

  Locale get defaultLocale {
    final locale = dotenv.get('DEFAULT_LOCALE', fallback: 'id');
    
    if (locale != 'id') {
      return const Locale('en', 'US');
    }
    
    return Locale(locale, locale.toUpperCase());
  }
}

intl_service.dart

代码语言:javascript
运行
复制
class IntlService extends GetxService {
  static IntlService get instance => Get.find();

  Locale get _locale {
    return Get.locale ?? EnvService.instance.defaultLocale;
  }

  Future<IntlService> init() async {
    await initializeDateFormatting(_locale.countryCode);
    return this;
  }

  String formatCurrency(double number) {
    final formatter = NumberFormat.simpleCurrency(decimalDigits: 0);
    return formatter.format(number);
  }

  String formatDate(DateTime? dateTime, [String? format]) {
    if (dateTime == null) {
      return '';
    }
    return DateFormat(format).format(dateTime);
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-14 20:54:29

尝试在GetMaterialApp中添加以下内容

代码语言:javascript
运行
复制
 localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74433986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档