我嘗試向用戶顯示警告,不要讓用戶名和密碼欄位為空,但我收到此錯誤:
E/flutter(6339):[錯誤:flutter/lib/ui/ui_dart_state.cc(198)]未處理的例外:型別'TextEditingController'不是型別轉換中'String'型別的子型別
這是我的代碼:
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,
),
),
),
),
)
],
),
),
),
),
],
),
),
),
);
}
}
uj5u.com熱心網友回復:
我認為下面的代碼有問題。
...
TextEditingController username = TextEditingController();
TextEditingController password = TextEditingController();
...
var response = await http.post(url, body: {
username : username.text,
password : password.text,
});
'username' 和 'password' 被定義為 TextEditingController 型別。
但是您將該值作為鍵插入到“發布”訊息正文中。
所以你需要像下面這樣改變密鑰。
var response = await http.post(url, body: {
'username' : username.text,
'password' : password.text,
});
uj5u.com熱心網友回復:
在鍵體中使用雙引號
var response = await http.post(url, body: {
"username" : username.text,
"password" : password.text,
});
另外,您對空欄位的檢查無效,最好使用它
username.text.isEmpty || password.text.isEmpty
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497919.html
上一篇:即使response.body有資料,snapshot.hasData回傳false[Flutter]
下一篇:使用異步功能更改通知提供程式
