flutter - Flutter 错误 - 断言失败:第 298 行 pos 10:'data != null':必须向 Text 小部件提供非空字符串。登录时
问题描述
我一直在使用rest api登录和注销的小项目中工作。但是每当我尝试登录时,我都会出错
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/text.dart': Failed assertion: line 298 pos 10: 'data != null': A non-null String must be provided to a Text widget.
出于测试目的,我的密码是 12345,但是当我将密码更改为仅字符串示例 abcd 时,当我将密码更改为字符串和数字混合时,它会起作用,生成相同的错误。这是颤振登录页面代码谢谢。
class LogIn extends StatefulWidget {
@override
_LogInState createState() => _LogInState();
}
class _LogInState extends State<LogIn> {
bool _isLoading = false;
TextEditingController mailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
ScaffoldState scaffoldState;
_showMsg(msg) { //
final snackBar = SnackBar(
content: Text(msg),
action: SnackBarAction(
label: 'Close',
onPressed: () {
// Some code to undo the change!
},
),
);
Scaffold.of(context).showSnackBar(snackBar);
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset : false,
body: Container(
child: Stack(
children: <Widget>[
/////////// background///////////
new Container(
decoration: new BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.0, 0.4, 0.9],
colors: [
Color(0xFFFF835F),
Color(0xFFFC663C),
Color(0xFFFF3F1A),
],
),
),
),
Positioned(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Card(
elevation: 4.0,
color: Colors.white,
margin: EdgeInsets.only(left: 20, right: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
///////////// Email//////////////
TextField(
style: TextStyle(color: Color(0xFF000000)),
controller: mailController,
cursorColor: Color(0xFF9b9b9b),
keyboardType: TextInputType.text,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.account_circle,
color: Colors.grey,
),
hintText: "Email",
hintStyle: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 15,
fontWeight: FontWeight.normal),
),
),
/////////////// password////////////////////
///passwordController
TextField(
style: TextStyle(color: Color(0xFF000000)),
cursorColor: Color(0xFF9b9b9b),
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
prefixIcon: Icon(
Icons.vpn_key,
color: Colors.grey,
),
hintText: "Password",
hintStyle: TextStyle(
color: Color(0xFF9b9b9b),
fontSize: 15,
fontWeight: FontWeight.normal),
),
),
/*TextFormField(
controller: passwordController,
cursorColor: Colors.white,
obscureText: true,
style: TextStyle(color: Colors.white70),
decoration: InputDecoration(
icon: Icon(Icons.lock, color: Colors.white70),
hintText: "Password",
border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.white70)),
hintStyle: TextStyle(color: Colors.white70),
),
),*/
///////////// LogIn Botton///////////////////
Padding(
padding: const EdgeInsets.all(10.0),
child: FlatButton(
child: Padding(
padding: EdgeInsets.only(
top: 8, bottom: 8, left: 10, right: 10),
child: Text(
_isLoading? 'Loging...' : 'Login',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
color: Color(0xFFFF835F),
disabledColor: Colors.grey,
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(20.0)),
onPressed: _isLoading ? null : _login,
),
),
],
),
),
),
//////////// new account///////////////
Padding(
padding: const EdgeInsets.only(top: 20),
child: InkWell(
onTap: () {
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => SignUp()));
},
child: Text(
'Create new Account',
textDirection: TextDirection.ltr,
style: TextStyle(
color: Colors.white,
fontSize: 15.0,
decoration: TextDecoration.none,
fontWeight: FontWeight.normal,
),
),
),
),
],
),
),
)
],
),
),
);
}
void _login() async{
setState(() {
_isLoading = true;
});
var data = {
'email' : mailController.text,
'password' : passwordController.text
};
var res = await CallApi().postData(data, 'login');
var body = json.decode(res.body);
if(body['message']==true){ // success to messsage
// _showMsg(body['message']);
print(body['accessToken']); // from token to accessToken
/*SharedPreferences localStorage = await SharedPreferences.getInstance();
localStorage.setString('token', body['token']);
localStorage.setString('user', json.encode(body['user']));*/
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => Home()));
}else{
_showMsg(body['message']);
}
setState(() {
_isLoading = false;
});
}
}
解决方案
令人惊叹的所有试图提供帮助的人,谢谢。我发现解决方案不是来自文本文件,而是来自服务器的响应不是 json 格式,它只是一个响应。我的错误来自这里
return response([
'success' => true,
'token' =>$token,
'user' =>$user,
'message'=>'Successfully Logged In'
]);
解决方案是 message .to string
return response()->json([
'success' => true,
'token' =>$token,
'user' =>$user,
'message'=>'Successfully Logged In'
]);
如果有人遇到问题并且不清楚,您可以问我。
推荐阅读
- .net-core - 不能包含“布尔”项的脚手架(EF,Core 2.1)模型
- c# - Wpf 数据绑定参数问题
- multithreading - Python 3.6:将在单独的 CMD 会话中运行相同的多线程 python 脚本绕过 Python 的 GIL 问题
- linux - 判断当前线程是否在 Linux x86 上被中断
- javascript - pdf.js mozilla lib问题以正确的顺序获取或提取纯文本
- javascript - 用javascript按住鼠标时如何运行函数
- css - A :focus-within 解决方法
- c++ - 如何在 C++ 中使用 yaml-cpp 中的环境变量
- excel - Excel 突出显示单元格中与其他单元格不同的部分文本
- firebase - 将 Stream Building 与特定的 Firestore 文档一起使用