FlutterのInvalid constant value.をなんとかしたい!
概要
今回の記事では、FlutterのInvalid constant value.をなんとかする手順を掲載する。
仕様書
環境
- Android Studio Giraffe | 2023.2.1 Patch 2
- Flutter 3.19.6
手順書
これはconstできないところにconstしちゃってるのが原因。
エラーが発生する例
TextEditingControllerを使ったTextFieldを例にする。
const TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'テキスト',
floatingLabelBehavior: FloatingLabelBehavior.always,
),
controller: textEditingController,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
),
エラーが発生しない例
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'テキスト',
floatingLabelBehavior: FloatingLabelBehavior.always,
),
controller: textEditingController,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
),
TextFieldのconstを外して、InputDecorationにconstを付けた。InputDecorationは不変なのでconstを付けてよい。
まとめ(感想文)
分かれば簡単なことなんだけども、用意されたウェジェットを使う場合にそのウェジェット自体にconstが必要なのか分からないことがあるのでちょいと困る。
