我想从颤振中显示这样的 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
我的代码怎么了? 状态部分有什么问题或对此有任何建议吗?因为我的 API 工作正常。 谢谢
答案 0 :(得分:2)
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;
}