0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Flutter】Arguments of a constant creation must be constant expressions.

Posted at

発生したエラー

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,
          ),
        ),
      ),
    );
  } 

解決策

  • Expandedconstを外す
    return Expanded(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 0.0),
  • constはコンパイル(プレ実行)時に値が決定する。
  • contentは実行時に値が決定している。
  • すなわちコンパイル時に値が決定してないものをconstの中で定義することは不可能ということ。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?