吸气剂“类型”在null上被调用。抖动错误

时间:2020-09-16 07:54:30

标签: flutter dropdown

我有这样的功能

  changingTheSubjectOptions() {
    if (registerType.type == "Teacher") {
      subjectsOptionShow = true;
    } else {
      subjectsOptionShow = false;
    }
    return subjectsOptionShow;
  }

当用户在此处选择下拉选项时,registerType.type的值在更改,

  RegisterType registerType;
  List<RegisterType> registrationList = <RegisterType>[
    RegisterType(type: "Student"),
    RegisterType(type: "Teacher"),
  ];

DropdownButton<RegisterType>(
                    value: registerType,
                    icon: Icon(Icons.arrow_downward),
                    iconSize: 24,
                    elevation: 16,
                    hint: Container(
                      // width: 200,
                      child: Text("You are ",
                          style: TextStyle(
                              color: Colors.grey,
                              fontWeight: FontWeight.w600,
                              fontSize: 15)),
                    ),
                    underline: Container(
                      color: Colors.white,
                    ),
                    style: TextStyle(color: Colors.black),
                    onChanged: (RegisterType newValue) {
                      setState(() {
                        registerType = newValue;
                      });
                    },
                    items: registrationList
                        .map<DropdownMenuItem<RegisterType>>(
                            (RegisterType value) {
                      return DropdownMenuItem<RegisterType>(
                        value: value,
                        child: Text(value.type),
                      );
                    }).toList(),
                  ),

现在当我这样调用函数时,

Visibility(
                visible: changingTheSubjectOptions(),
                

出现错误,
将吸气剂“类型”调用为null。
到目前为止,我知道registerType.type为空。
而且在初始化initState中的registerType.type之后,它也会出现相同的错误。

  void initState() {
    registerType.type = "Student";
    super.initState();
  }

如何初始化?

1 个答案:

答案 0 :(得分:0)

registerType为null,而不是registerType.type。也许将其初始化为某种东西或将其用于null检查。

这可能会消除您的错误:

    if (registerType?.type == "Teacher") {