2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Flutter/Dart】error: The argument type 'dynamic Function()' can't be assigned to the parameter type 'String Function(String)'. の対処法

Posted at

はじめに

Flutterで関数を引数として渡した時、次のようなエラーが出ることがあります。

class CustomTextFormField extends StatelessWidget{
  const CustomTextFormField({
    Key key,
    this.validator
  })

  final Function() validator;

  @override 
  Widget build(BuildContext context){
    return TextFormField(
      validator: validator, // <- ここでエラーが出る
    );
  }
}
error: The argument type 'dynamic Function()' can't be assigned to the parameter type 'String Function(String)'. 

メッセージ通り、String Function(String)型な関数が必要なのに、dynamic Function()型として関数を受け取っているためエラーがでています。

対処方法

関数の引数・返り値を指定してあげれば、エラーが解消します。

class CustomTextFormField extends StatelessWidget{
  const CustomTextFormField({
    Key key,
    this.validator
  })

  final String Function(String) validator;// <- 返り値と引数を指定する

  @override 
  Widget build(BuildContext context){
    return TextFormField(
      validator: validator, 
    );
  }
}

参考文献

Diagnostic messages | Dart

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?