【问题标题】:flutter class constructors error - "@required this."扑动类构造函数错误 - “@required this。”
【发布时间】:2021-07-18 08:10:33
【问题描述】:

在一个类的构造函数中,我遇到了几个错误,因为它说默认分配了一个空值并且不允许该值。我也无法初始化它们,因为它们已经修改了最终版本。我把代码和错误信息留给你。提前致谢

代码:

{

  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,
                     ^^^^^^^^^^^^^^

【问题讨论】:

    标签: flutter dart constructor default-constructor


    【解决方案1】:

    删除@

     {
      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);
    

    【讨论】:

    • 我已经看到@required 或 required 使用了几次,但从未理解其中的区别
    • 不客气,请考虑将此标记为您问题的解决方案。您在函数的可选参数中使用@required。
    • 德纳达! @grafeno30
    • 非常感谢@Huthaifa
    • @tazboy 这是真的。对于可选参数,您必须使它们可以为空。否则,如果它没有默认值或者不能为null,则必须使用required,以便在调用构造函数时传递一个值。
    猜你喜欢
    • 2012-07-01
    • 2013-08-04
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多