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】Invalid constant value.

Last updated at Posted at 2025-10-17

発生したエラー

Inavalid Constant Value

該当コード

edit_title_view.dart
class EditTitleViewState extends State<EditTitleView> {
  final TextEditingController _titleController = TextEditingController();
  var text = "";

  void init() {
    text = "this is sample text.";
  }

// 中略 buildメソッドの中でcustomTextField(text)を呼び出している

    Widget customTextField(String content) {
        return Expanded(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 0.0),
            child: TextFormField(
                controller: _titleController,
                maxLines: null,
                expands: true,
                decoration: const InputDecoration(
                  contentPadding: EdgeInsets.zero, 
                  hintText: content, // Invalid constant value.
                  hintStyle: TextStyle(
                    color: Colors.grey,
                    fontSize: 13
                  ),
                  border: InputBorder.none,
                ),
                style: TextStyle(
                  fontSize: 13,
                  fontWeight: FontWeight.w300
                ),
                onFieldSubmitted: (value){
                  // do something
                },
            ),
          )
        );
      }
}

解決策

  • InputDecorationconstを外す
decoration: InputDecoration(
              contentPadding: EdgeInsets.zero, 
              hintText: content,
  • const: コンパイル(プレ実行)時に値が決定する。
  • text/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?