【Flutter】「The method ‘FlatButton’ isn’t defined for the type」の原因と対策

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

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

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

The method 'FlatButton' isn't defined for the type 'TodoDetailState'. (Documentation)  Try correcting the name to the name of an existing method, or defining a method named 'FlatButton'.

Flutterの新バージョンで「FlatButton」が廃止になったことが原因でした。
代わりに「TextButton」を使用することでエラーが解消されました。

修正前

FlatButton(
  child: Text("OK"),
  onPressed: () => Navigator.pop(context),
),

修正後

TextButton(
  child: Text("OK"),
  onPressed: () => Navigator.pop(context),
),

関連ページ

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

コメント