14
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Flutter] Unsupported operation: Cannot set value in unmodifiable Map の対処方法

Last updated at Posted at 2020-02-19

何が起きた

Flutterで多言語化設定をしたかったので、次のようなコードを書いたところ、エラーが出ました。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: const [
        Locale('en'),
        Locale('ja'),
      ],
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
    );
  }
}
Unsupported operation: Cannot set value in unmodifiable Map

原因

次のようにアプリを起動していたのですが、どうも多言語化設定とinitializeDateFormatting()が衝突しているらしく(多言語化設定時に、initializeDateFormatting()相当の処理が行われる)、これが原因でした。

void main() {
  initializeDateFormatting().then(
    (dynamic _) => runApp(MyApp()),
 );
}

対処方法

initializeDateFormatting()を呼び出すのをやめるだけで、解決します。

参考資料

Flutterの多言語対応を試してみた | backport
Dart/Flutter での多言語対応あれこれ - Flutter 🇯🇵 - Medium
Issue with showDatePicker and locale · Issue #16304 · flutter/flutter · GitHub

14
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?