gpt4 book ai didi

flutter - 登录在空上调用了 '_mulFromInteger'方法

转载 作者:行者123 更新时间:2023-12-03 03:00:28 31 4
gpt4 key购买 nike

我已经在提供者中登录了method。

  Future<void> signIn(
String email, String password, BuildContext context) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
final clientID = "com.super.app";
final body = "username=$email&password=$password&grant_type=password";
final String clientCredentials =
const Base64Encoder().convert("$clientID:".codeUnits);

try {
final http.Response response =
await http.post("http://localhost:8888/auth",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic $clientCredentials"
},
body: body);
final jsonResponse = json.decode(response.body);
// if (jsonResponse["error"] != null) {
// throw HttpException(jsonResponse["error"]);
// }
_userId = 1;
_token = jsonResponse['access_token'];
_expiryDate = DateTime.now().add(
Duration(
seconds: jsonResponse['expires_in'],
),
);
_autoLogout();
notifyListeners();
final userData = json.encode(
{
'userId': 1,
'email': email,
'token': _token,
'expiryDate': _expiryDate.toIso8601String(),
},
);
sharedPreferences.setString('userData', userData);
} catch (error) {
print(error.toString()); //<-- misleading error
}
}

一切正常,但是如果传递了错误的登录凭据,那么我会产生误导性错误
flutter: NoSuchMethodError: The method '_mulFromInteger' was called on null.
Receiver: null
Tried calling: _mulFromInteger(1000000)

后端传递错误代码400和正文{“error”:“invalid client”},但是我得到了奇怪的错误作为输出。那么这个错误是什么意思,为什么我得到了这个而不是 body

最佳答案

从@Suragch注释起,我的代码几乎没有问题。首先,我认为服务器返回400代码时,它将自动引发错误并跳过其余各行。.我错了,所以基本上我必须取消对HTTP异常的代码注释,并在按钮中捕获错误

  try {
await Provider.of<Auth>(context, listen: false).signIn(
emailController.text, passwordController.text);
} on HttpException catch (error) {
print(error.toString());
} catch (error) {
print(error);
}

关于flutter - 登录在空上调用了 '_mulFromInteger'方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61520598/

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