Home > Software engineering >  E/flutter ( 6339): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Text
E/flutter ( 6339): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'Text

Time:07-02

I try to show an alert for the user to not let username and passweord fields empty, but I receive this error:

E/flutter ( 6339): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'TextEditingController' is not a subtype of type 'String' in type cast

here is my code:

class _MyHomePageState extends State<MyHomePage> {
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();

Future login(BuildContext cont) async {

if(username.text == " " || password.text == " "){
  Alert(
    context: context,
    type: AlertType.error,
    title: "ALERT",
    desc: "Wrong username or password",
    buttons: [
      DialogButton(
        child: Text(
          "OK",
          style: TextStyle(color: Colors.white, fontSize: 20),
        ),
        onPressed: () => Navigator.pop(context),
        width: 120,
      )
    ],
  ).show();

}
else{
  var url = "localhost:8080/localconnect/login.php";
  var response = await http.post(url, body: {
    username : username.text,
    password : password.text,
  });

  var data = jsonDecode(response.body);

  if(data=="success"){
    Navigator.push(cont, MaterialPageRoute(builder: (context)=>WelcomeScreen()));
  }
  else{
    Alert(
      context: context,
      type: AlertType.error,
      title: "ALERT",
      desc: "Wrong username or password",
      buttons: [
        DialogButton(
          child: Text(
            "OK",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          onPressed: () => Navigator.pop(context),
          width: 120,
        )
      ],
    ).show();
  }
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
  body: SingleChildScrollView(
    child: Container(
      constraints: BoxConstraints(
        maxHeight: MediaQuery.of(context).size.height,
        maxWidth: MediaQuery.of(context).size.width,
      ),
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
            Colors.blue[800]!,
            Colors.blue[600]!,
          ],
          begin: Alignment.topLeft,
          end: Alignment.centerRight,
        ),

      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(flex: 2,
            child: Padding(
              padding: const EdgeInsets.symmetric(
                  vertical: 36.0, horizontal: 24.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    "Login",
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 46.0,
                      fontWeight: FontWeight.w800
                    ),
                  ),
                  SizedBox(height: 10.0,),
                  Text(
                    "Enter to system",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 22.0,
                        fontWeight: FontWeight.w300
                    ),
                  )
                ],
              ),
            )
          ),
          Expanded(flex: 5,
            child: Container(
              width: double.infinity,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(40),
                  topRight: Radius.circular(40),)
              ),
              child: Padding(
                padding: const EdgeInsets.all(24.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    TextField(
                      controller: username,
                      keyboardType: TextInputType.name,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8.0,),
                          borderSide: BorderSide.none,
                        ),
                        filled: true,
                        fillColor: Color(0xffe7edeb),
                        hintText: "Username",
                        prefixIcon: Icon(
                          Icons.person,
                          color: Colors.grey[600],
                        ),

                      ),
                    ),
                    SizedBox(height: 20.0,),
                    TextField(
                      controller: password,
                      obscureText: true,
                      // keyboardType: TextInputType.visiblePassword,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8.0,),
                          borderSide: BorderSide.none,
                        ),
                        filled: true,
                        fillColor: Color(0xffe7edeb),
                        hintText: "Password",
                        prefixIcon: Icon(
                          Icons.lock,
                          color: Colors.grey[600],
                        ),

                      ),
                    ),
                    SizedBox(height: 50.0,),
                    Container(
                      width: double.infinity,
                      child: ElevatedButton(
                        onPressed: (){
                          login(context);
                        },

                        // color: Colors.blue[800],
                        child: Padding(
                          padding: const EdgeInsets.symmetric(vertical: 16.0),
                          child: Text(
                            "Login",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 16.0,
                            ),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),

          ),
        ],
      ),
    ),
  ),
);
}
}

CodePudding user response:

I think below code makes a problem.

...
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();

...

var response = await http.post(url, body: {
    username : username.text,
    password : password.text,
  });

'username' and 'password' is defined as a TextEditingController type.
But you insert that value to 'post' message body as a key.


So you need to change key like below.
var response = await http.post(url, body: {
    'username' : username.text,
    'password' : password.text,
  });

CodePudding user response:

use double quotation in key body

var response = await http.post(url, body: {
  "username" : username.text,
  "password" : password.text,
});

also, your check of empty fields is not valid, is better to use this

username.text.isEmpty || password.text.isEmpty
  • Related