flutter class 构造函数错误 - “@required this。”
flutter class constructors error - "@required this."
在 class 的构造函数中,我遇到了几个错误,因为它说默认分配了一个空值,并且不允许该值。我也无法初始化它们,因为它们已经进行了最终修改。我把代码和错误信息留给你。提前致谢
代码:
{
final IconData icon;
final String placeholder;
final TextEditingController textController;
final TextInputType keyboardType;
final bool isPassword;
const CustomInput ({
Key key,
@required this.icon,
@required this.placeholder,
@required this.textController,
this.keyboardType = TextInputType.text,
this.isPassword = false
}) : super(key:key);
错误:
lib/widgets/custom_input.dart:13:11: Error: The parameter 'key' can't have a value of 'null' because of its type 'Key', but the implicit default value is 'null'.
- 'Key' is from 'package:flutter/src/foundation/key.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/foundation/key.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
Key key,
^^^
lib/widgets/custom_input.dart:14:22: Error: The parameter 'icon' can't have a value of 'null' because of its type 'IconData', but the implicit default value is 'null'.
- 'IconData' is from 'package:flutter/src/widgets/icon_data.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/icon_data.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
@required this.icon,
^^^^
lib/widgets/custom_input.dart:15:22: Error: The parameter 'placeholder' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
@required this.placeholder,
^^^^^^^^^^^
lib/widgets/custom_input.dart:16:22: Error: The parameter 'textController' can't have a value of 'null' because of its type 'TextEditingController', but the implicit default value is 'null'.
- 'TextEditingController' is from 'package:flutter/src/widgets/editable_text.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
@required this.textController,
^^^^^^^^^^^^^^
删除 @
{
final IconData icon;
final String placeholder;
final TextEditingController textController;
final TextInputType keyboardType;
final bool isPassword;
const CustomInput ({
required Key key,
required this.icon,
required this.placeholder,
required this.textController,
this.keyboardType = TextInputType.text,
this.isPassword = false
}) : super(key:key);
在 class 的构造函数中,我遇到了几个错误,因为它说默认分配了一个空值,并且不允许该值。我也无法初始化它们,因为它们已经进行了最终修改。我把代码和错误信息留给你。提前致谢
代码:
{
final IconData icon;
final String placeholder;
final TextEditingController textController;
final TextInputType keyboardType;
final bool isPassword;
const CustomInput ({
Key key,
@required this.icon,
@required this.placeholder,
@required this.textController,
this.keyboardType = TextInputType.text,
this.isPassword = false
}) : super(key:key);
错误:
lib/widgets/custom_input.dart:13:11: Error: The parameter 'key' can't have a value of 'null' because of its type 'Key', but the implicit default value is 'null'.
- 'Key' is from 'package:flutter/src/foundation/key.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/foundation/key.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
Key key,
^^^
lib/widgets/custom_input.dart:14:22: Error: The parameter 'icon' can't have a value of 'null' because of its type 'IconData', but the implicit default value is 'null'.
- 'IconData' is from 'package:flutter/src/widgets/icon_data.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/icon_data.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
@required this.icon,
^^^^
lib/widgets/custom_input.dart:15:22: Error: The parameter 'placeholder' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
@required this.placeholder,
^^^^^^^^^^^
lib/widgets/custom_input.dart:16:22: Error: The parameter 'textController' can't have a value of 'null' because of its type 'TextEditingController', but the implicit default value is 'null'.
- 'TextEditingController' is from 'package:flutter/src/widgets/editable_text.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
@required this.textController,
^^^^^^^^^^^^^^
删除 @
{
final IconData icon;
final String placeholder;
final TextEditingController textController;
final TextInputType keyboardType;
final bool isPassword;
const CustomInput ({
required Key key,
required this.icon,
required this.placeholder,
required this.textController,
this.keyboardType = TextInputType.text,
this.isPassword = false
}) : super(key:key);