Flutter 参数不能有空值

Flutter The parameter can't have a value of null

我的队友有一个问题,flutter 报告说我的变量不能有 null 值,但是在我的 mac 上一切正常。我们在代码中找不到任何差异。有谁知道哪个问题可能导致这个

问题基本上针对构造函数和变量

我使用的是 Macbook pro,其他不工作的是 windows 笔记本电脑

class DateTextField extends StatelessWidget {
  DateTextField({@required this.dateComposition, @required this.dateCompositionHintText, this.onFieldSubmitted, this.onChanged, this.focusNode});

  //dateComposition can only have Day, Month, or Year as strings
  final String dateComposition;
  final String dateCompositionHintText;
  final Function onFieldSubmitted;
  //onchanged function has to be determined if you want to automatically set the focus to another text field, see application on age_screen with the datetextfields
  final Function onChanged;
  //set the widget with its focusnode
  final FocusNode focusNode;


  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Text(
          dateComposition,
          style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
        ),
        SizedBox(height: 7),
        Container(
          margin: EdgeInsets.all(4),
          width: dateComposition == "Year"? 73: 55,
          child: TextFormField(
            textAlign: TextAlign.center,
            //Keyboardtype for numbers
            keyboardType: TextInputType.number,
            //only numbers can be typed in
            inputFormatters: <TextInputFormatter>[
              FilteringTextInputFormatter.digitsOnly,
              LengthLimitingTextInputFormatter(dateComposition=="Year"? 4 : 2),
            ],
            style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
            autofocus: true,
            cursorColor: kPrimaryMagentaColor,
            onFieldSubmitted: onFieldSubmitted,
            onChanged: onChanged,
            focusNode: focusNode,
            decoration: InputDecoration(
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: kTextIconColorDarkBlue),
                borderRadius: BorderRadius.circular(15),
              ),
              hintText: dateCompositionHintText,
              hintStyle: TextStyle(fontWeight: FontWeight.w600, fontSize: 18.0),
              contentPadding: EdgeInsets.only(
                left: 10,
                right: 10,
                top: 10,
                bottom: 10,
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
                borderSide: BorderSide(
                  color: kPrimaryMagentaColor,
                  width: 1.5,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

  1. 请检查您设备中的 dart 和 flutter 版本
  2. 检查 pubspec.yaml 中使用的软件包版本是否相同,或者当你的队友进行 pub 升级或​​ pub get 时它是否改变了。

编辑: 我在这一行出现变量不能为空的错误

  DateTextField({@required this.dateComposition, @required this.dateCompositionHintText, this.onFieldSubmitted, this.onChanged, this.focusNode});

我做了以下更改以消除错误

  DateTextField(
      {required this.dateComposition,
      required this.dateCompositionHintText,
      required this.onFieldSubmitted,
      required this.onChanged,
      required this.focusNode});

如果您的队友在同一行遇到错误,请尝试此操作。

编辑 2:

class DateTextField extends StatelessWidget {
  DateTextField(
      {required this.dateComposition,
      required this.dateCompositionHintText,
      this.onFieldSubmitted,
      this.onChanged,
      this.focusNode});

  //dateComposition can only have Day, Month, or Year as strings
  final String dateComposition;
  final String dateCompositionHintText;
  final Function ?onFieldSubmitted;
  //onchanged function has to be determined if you want to automatically set the focus to another text field, see application on age_screen with the datetextfields
  final Function ?onChanged;
  //set the widget with its focusnode
  final FocusNode focusNode;
}