Help us understand the problem. What is going on with this article?

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

何が起きた

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

welchi
Flutterと機械学習が好きなバーチャル美少女エンジニアです。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした