発生したエラー
Arguments of a constant creation must be constant expressions.
Try making the argument a valid constant, or use 'new' to call the constructor.dart(const_with_non_constant_argument)
該当コード
edit_title_view.dart
var text = "";
void init() {
text = "this is sample text.";
}
// 中略。customTextLabel(text)を呼び出している。
Widget customTextLabel(String content) {
return const Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 0.0),
child: Text(
content, // error
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w300,
),
),
),
);
}
解決策
-
Expandedのconstを外す
return Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 0.0),
-
constはコンパイル(プレ実行)時に値が決定する。 -
contentは実行時に値が決定している。 - すなわちコンパイル時に値が決定してないものをconstの中で定義することは不可能ということ。