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】Extensions can't declare instance fields. Try replacing the field with a getter.

Last updated at Posted at 2025-10-16

操作

  • extensionの中に定数を置いた。

発生したエラー

Extensions can't declare instance fields. Try replacing the field with a getter.
edit_title_view.dart
class EditTitleViewState extends State<EditTitleView> {
  @override
  Widget build(BuildContext context) {
    return customTextField();
  }
}

extension EditTitleViewComponents on EditTitleViewState {
  late TextEditingController _titleController;
}

解決策

  • 常識ではあるんだけれどもflutterに関係なくextensionの中で定数を定義することができないため、元のクラスにて定義してあげる必要がある。
edit_title_view.dart
class EditTitleViewState extends State<EditTitleView> {
  // OK: 
  late TextEditingController _titleController;
  @override
  Widget build(BuildContext context) {
    return customTextField();
  }
}

extension EditTitleViewComponents on EditTitleViewState {
    //NG: late TextEditingController _titleController;
}
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?