作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 flutter 中显示这样的 json
{"status":true,"message":"Successfully Login!","acc_id":"2","email":"cikananda2020@gmail.com","password":"9a365b0597e198ceac41966db1d6f47de66a86bb99e3e5a811c3030"}
但是当我从 flutter 点击登录时,我得到了这个结果
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The getter 'status' was called on null.
这是我的登录功能
void login() async {
UserResults userResults;
await UserResults.sqlLogin(
email: email, password: password,url: BaseURL.kLoginUrl)
.then((value) => userResults = value as UserResults);
print(userResults.status);
if (userResults.status == true) //error start here {
SharedPref.simpanPrefereneces(
userResults.email);
Navigator.pushReplacementNamed(context, HomeScreen.id);
print(userResults.message);
} else {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text(userResults.message),
duration: Duration(seconds: 3),
));
}
}
这是我的 SqlLogin
static Future<UserResults> sqlLogin(
{String email, String password, String idUser, String url}) async {
var url = "http://192.168.0.23/Api/login.php?email=" + email + "&password=" + password;
final response = await http.get(url,headers:{"Content-Type":
"application/json"});
Map<String, dynamic> data = new Map<String, dynamic>.from(json.decode(response.body));
print(data['email']);
print(data['password']);
print(data['acc_id']);
print(data['status']);
}
}
这是我的控制台日志
I/flutter ( 7370): cikananda2020@gmail.com
I/flutter ( 7370): 9a365b0597e198ceac41966db1d6f47de66a86bb99e3e5a811c3030
I/flutter ( 7370): 2
I/flutter ( 7370): true
E/flutter ( 7370): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: NoSuchMethodError: The getter 'status' was called on null.
E/flutter ( 7370): Receiver: null
E/flutter ( 7370): Tried calling: status
我的代码怎么了?
最佳答案
status 为空,因为您没有在 sqlLogin
中返回数据.
由于您想将数据转换为 UserResults
,您不需要将它们转换为 Map
.
static Future<UserResults> sqlLogin({String email, String password, String idUser, String url}) async {
var url = "http://192.168.0.23/Api/login.php?email=" + email + "&password=" + password;
final response = await http.get(url,headers:{"Content-Type":
"application/json"});
var res = UserResults.fromJson(json.decode(response.body));
return res;
}
关于flutter - NoSuchMethodError : The getter 'status' was called on null on flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67532897/
我是一名优秀的程序员,十分优秀!