必须为文本窗口小部件错误提供非空字符串

时间:2020-09-14 19:49:44

标签: mysql flutter

您好,我是新手,我是第一次收到此错误! ,我实际上不知道为什么,但是它很奇怪,因为它运行得很好 错误是:必须为文本窗口小部件提供非空字符串。 'package:flutter / src / widgets / text.dart': 断言失败:第360行pos 10:'data!= null' 我的注册状态代码:

class Register extends StatefulWidget {
  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {
  TextEditingController email = TextEditingController();
  TextEditingController password = TextEditingController();
  TextEditingController name = TextEditingController();
  TextEditingController year = TextEditingController();
  TextEditingController type = TextEditingController();
  final _key = GlobalKey<FormState>();
  bool _secureText = true ;
  showHide(){
    setState(() {
      _secureText=!_secureText;
    });
  }
  check(){
    final form = _key.currentState;
    if(form.validate()){
      form.save();
      register();
    }
  }
  void register()async{
    final response = await http.post('http://msc-mu.com/register.php',body: {
      'email':email.text,
      'password':password.text,
      'name':name.text,
      'year':year.text,
      'type':type.text
    });
    final userdata = json.decode(response.body);
    String emailApi = userdata[0]['email'];
    String nameAPI = userdata[0]['name'];
    String id = userdata[0]['id'];
    String yearAPI = userdata[0]['year'];
    String typeAPI = userdata[0]['type'];
    setState(() {
      savePref(token, emailApi, nameAPI, id, yearAPI, typeAPI);
      print(savePref(token, emailApi, nameAPI, id, yearAPI, typeAPI));
    });
    return userdata;
  }
  var token ;
  savePref(String token, String email, String name,String id, String year, String type)async{
    SharedPreferences pref = await SharedPreferences.getInstance();
    setState(() {
      pref.setString('logged', token);
      pref.setString('email', email);
      pref.setString('name', name);
      pref.setString('id', id);
      pref.setString('year', year);
      pref.setString('type', type);
      pref.commit();
    });

  }
  String _selectYear ;
  List years = List();
  Future<String> getYears()async{
    final response = await http.get('http://msc-mu.com/getYears.php');
    var resBody = json.decode(response.body);
setState(() {
  years = resBody ;

});  }
  String _selectType; 
  List types = List();
  Future<String> getTypes ()async{
    final response = await http.post('http://msc-mu.com/getlevel.php');
    var resBody = json.decode(response.body);
setState(() {
  types = resBody;

});
  }
  @override
  void initState() {
    // TODO: implement initState

     year.text = _selectYear;
     type.text = _selectType;
     this.getYears();
    this.getTypes();
     super.initState();

  }


  @override
  Widget build(BuildContext context) {
    return  Scaffold(

      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          child: Column(
            children: <Widget>[
              ClipPath(
                clipper: OvalBottomBorderClipper(),
                child: Image(
                  image: AssetImage('images/logo1.png'),
                  width: double.infinity,
                  height: MediaQuery.of(context).size.height / 3.5,
                  fit: BoxFit.cover,
                ),
              ),
              Form(
                key: _key,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      height: 25.0,
                    ),
                    Card(
                      elevation: 6.0,
                      child: TextFormField(
                        controller: name,
                        validator: MinLengthValidator(8,
                            errorText: ('Name must be at least 8 digit long')),
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontWeight: FontWeight.w300,
                        ),
                        decoration: InputDecoration(
                            prefixIcon: Padding(
                              padding: EdgeInsets.only(left: 20, right: 15),
                              child: Icon(Icons.person, color: Colors.black),
                            ),
                            contentPadding: EdgeInsets.all(18),
                            labelText: "FullName"),
                      ),
                    ),
                    Card(
                      elevation: 6.0,
                      child: TextFormField(
                        controller: email,
                        validator: EmailValidator(
                            errorText: 'Please enter a valid email address'),
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontWeight: FontWeight.w300,
                        ),
                        decoration: InputDecoration(
                            prefixIcon: Padding(
                              padding: EdgeInsets.only(left: 20, right: 15),
                              child: Icon(Icons.email, color: Colors.black),
                            ),
                            contentPadding: EdgeInsets.all(18),
                            labelText: "Email"),
                      ),
                    ),
                    Card(
                      elevation: 6.0,
                      child: TextFormField(
                        controller: password,
                        validator: MultiValidator([
                          RequiredValidator(errorText: 'Password is Required'),
                          MinLengthValidator(8,
                              errorText:
                              'Password must be at least 8 digit long')
                        ]),
                        obscureText: _secureText,
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontWeight: FontWeight.w300,
                        ),
                        decoration: InputDecoration(
                            suffixIcon: IconButton(
                              onPressed: showHide,
                              icon: Icon(_secureText
                                  ? Icons.visibility_off
                                  : Icons.visibility),
                            ),
                            prefixIcon: Padding(
                              padding: EdgeInsets.only(left: 20, right: 15),
                              child: Icon(Icons.phonelink_lock,
                                  color: Colors.black),
                            ),
                            contentPadding: EdgeInsets.all(18),
                            labelText: "Password"),
                      ),
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children:<Widget> [
                        Card(
                          elevation: 6.0,
                          child: DropdownButton<String>(
                            hint: Text('Choose your role'),
                            icon: Icon(Icons.arrow_drop_down),
                            iconSize: 24.0,
                            elevation: 16,
                            style: TextStyle(color: Colors.black, fontSize: 16.0),
                            underline: Container(
                              height: 2,
                              color: Theme.of(context).primaryColor,
                            ),
                            items: types.map((item) {
                              return DropdownMenuItem(
                                child: Text(item['levelname']),
                                value: item['levelname'].toString(),
                              );
                            }).toList(),
                            onChanged: (newVal) {
                              setState(() {
                                _selectType = newVal;
                                type.text = _selectType;
                              });
                            },
                            value: _selectType,
                          ),

                        ),
                        Card(
                          elevation: 6.0,
                          child: DropdownButton<String>(
                            hint: Text('Choose your Year'),
                            icon: Icon(Icons.arrow_drop_down),
                            iconSize: 24.0,
                            elevation: 16,
                            style: TextStyle(color: Colors.black, fontSize: 16.0),
                            underline: Container(
                              height: 2,
                              color: Theme.of(context).primaryColor,
                            ),
                            items: years.map((item) {
                              return DropdownMenuItem(
                                child: Text(item['name']),
                                value: item['name'].toString(),
                              );
                            }).toList(),
                            onChanged: (newVal){
                              setState(() {
                                 _selectYear = newVal ;
                                year.text = _selectYear;

                              });

                            },
                            value: _selectYear,
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 20.0,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        SizedBox(
                          width: 150.0,
                          height: 44.0,
                          child: RaisedButton(
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                  BorderRadius.circular(15.0)),
                              child: Text(
                                "Register",
                                style: TextStyle(fontSize: 18.0),
                              ),
                              textColor: Colors.white,
                              color: Theme.of(context).primaryColor,
                              onPressed: () {
                                check();
                              }),
                        ),
                        SizedBox(
                          width: 150.0,
                          height: 44.0,
                          child: RaisedButton(
                              shape: RoundedRectangleBorder(
                                  borderRadius:
                                  BorderRadius.circular(15.0)),
                              child: Text(
                                "GoTo Login",
                                style: TextStyle(fontSize: 18.0),
                              ),
                              textColor: Colors.white,
                              color: Theme.of(context).primaryColor,
                              onPressed: () {
                                Navigator.push(
                                  context,
                                  MaterialPageRoute(
                                      builder: (context) => Login()),
                                );
                              }),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),


        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

有2件事:

  • item['levelname']为空:您的API调用返回Levelname(大写字母L
  • item['name']也为null,因为它不存在于API调用返回的js中。