【Flutter】「The getter ‘accentColor’ isn’t defined for the type ‘ThemeData’.」の原因と対策

Flutterのエラー「The getter ‘accentColor’ isn’t defined for the type ‘ThemeData’.」の原因と対策について紹介します。

「The method ‘showSnackBar’ isn’t defined for the type ‘ScaffoldState’. 」の原因と対策

Flutterでデバッグすると以下のエラーが出ました。

The getter 'accentColor' isn't defined for the type 'ThemeData'. (Documentation)  Try importing the library that defines 'accentColor', correcting the name to the name of an existing getter, or defining a getter or field named 'accentColor'.

accentColorがFlutter v2.3.0 -0.1.pre 以降から非推奨になったことが原因でした。
代わりに「colorScheme.secondary」を使用することでエラーが解消されました。

修正前

Theme.of(context).colorScheme.secondary

修正後

Theme.of(context).accentColor

関連ページ

【Flutter超入門】使い方とサンプルアプリを解説
Flutterの使い方とサンプルアプリ(iOS、Android、Windows)について入門者向けに解説します。

コメント