Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@welchi

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

More than 1 year has passed since last update.

何が起きた

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

1
Help us understand the problem. What is going on with this article?
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
welchi
株式会社GoodWelchi代表。Flutterと機械学習が好きです。お仕事等のお問い合わせは welchizm@gmail.com まで。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?