🤳

ダブルタップすると落ちるバグの解決方法【Flutter / Dart】

2021/12/11に公開

はじめまして、ますみです!

株式会社Galirage(ガリレージ)という「生成AIのシステム開発会社」で、代表をしております^^

自己紹介.png

最近、Flutterのアプリ上で、TextFieldのようなInputするUIをダブルタップすると、エラーが起きて、アプリが落ちるというバグに遭遇しました。

調査をしたところ、どうやら、localizationsDelegatesの設定の問題でした。

結構Criticalなバグであるため、同じ問題に遭遇している人たちに向けて、対処法を書き留めます。

エラー内容

コンソール上のエラー内容は以下の通りです。

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building _CupertinoTextSelectionControlsToolbar(dirty, dependencies: [MediaQuery, _LocalizationsScope-[GlobalKey#09699]], state: _CupertinoTextSelectionControlsToolbarState#62b54):
No CupertinoLocalizations found.

_CupertinoTextSelectionControlsToolbar widgets require CupertinoLocalizations to be provided by a Localizations widget ancestor.
The cupertino library uses Localizations to generate messages, labels, and abbreviations.

To introduce a CupertinoLocalizations, either use a CupertinoApp at the root of your application to include them automatically, or add a Localization widget with a CupertinoLocalizations delegate.

エラーが起きた時のエラー箇所は以下の通りです。

bool debugCheckHasCupertinoLocalizations(BuildContext context) {
  assert(() {
    if (Localizations.of<CupertinoLocalizations>(context, CupertinoLocalizations) == null) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('No CupertinoLocalizations found.'),
        ErrorDescription(
          '${context.widget.runtimeType} widgets require CupertinoLocalizations '
          'to be provided by a Localizations widget ancestor.',
        ),
        ErrorDescription(
          'The cupertino library uses Localizations to generate messages, '
          'labels, and abbreviations.',
        ),
        ErrorHint(
          'To introduce a CupertinoLocalizations, either use a '
          'CupertinoApp at the root of your application to include them '
          'automatically, or add a Localization widget with a '
          'CupertinoLocalizations delegate.',
        ),
        ...context.describeMissingAncestor(expectedAncestorType: CupertinoLocalizations),
      ]);
    }
    return true;
  }());
  return true;
}

どうやらCupertinoに関するエラーのようですね。

解決方法

解決方法としては、MaterialAppを定義している箇所で、以下のようにlocalizationsDelegatesの設定として、次の三つを追加すると直りました。

main.dart
return MaterialApp(
  ...
  localizationsDelegates: const [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
    GlobalCupertinoLocalizations.delegate,
  ],
  supportedLocales: const [
    Locale('ja'),
  ],
);

最後に

最後まで読んでくださり、ありがとうございました!
この記事を通して、少しでもあなたの学びに役立てば幸いです!

おまけ①:生成AIアカデミー

より専門的な「生成AIエンジニア人材」を目指しませんか?

そんな方々に向けて、「生成AIアカデミー(旧:生成AIエンジニア塾)」というプログラムを始めました🎉

最終的なゴールとして、『エンタープライズ向けの生成AIシステムを構築するためのスキルを習得し、大手案件で活躍できる人材』を目標とします。

また、一人一人にしっかりと向き合って、メンタリングをできるようにするため、現在メンバーの人数制限をしております。本気度やスキルレベルの高い人から、順番にご案内しております。

▼ 登録はこちらから ▼
https://bit.ly/generative_ai_engineer_school_by_zenn

おまけ②:AI Newsletter for Biz

最新のAIニュースの情報を収集しませんか?

AI Newsltter for Bizは、ビジネスパーソン向けに「AIニュース」を定期配信する完全無料のニュースレターです📩

一人でも多くの方にとって、「AI人材としてのスキルアップ」につながれば幸いです^^

また、現在、登録者限定で「明日から使える 無料AIサービス3選」のPDFを配布中です 🎁
※ ご登録完了のメールに、PDFリンクを添付いたします。

期間限定のプレゼントとなりますので、ぜひ、お早めにご登録ください!

▼ 登録はこちらから ▼
https://bit.ly/ai_newsletter_for_biz_zenn

Discussion