gpt4 book ai didi

flutter - NoSuchMethodError : The getter 'status' was called on null on flutter

转载 作者:行者123 更新时间:2023-12-04 07:40:01 45 4
gpt4 key购买 nike

我想从 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
我的代码怎么了?
状态部分有什么问题或对此有任何建议吗?因为我的 API 工作正常。
谢谢你

最佳答案

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/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com