未处理的异常:无效参数(值):不能为 null

发布于 2025-01-10 10:41:55 字数 1758 浏览 0 评论 0原文

我正在尝试通过观看来自 https://www.flutterauthentication.org 的视频来登录 flutter 身份验证。 youtube.com/watch?v=2DtFGF2v_vk&t=365s 但我收到一条错误消息,

Reloaded 1 of 975 libraries in 954ms.
E/flutter ( 8369): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument(s) (value): Must not be null
E/flutter ( 8369): #0      ArgumentError.checkNotNull (dart:core/errors.dart:207:27)
E/flutter ( 8369): #1      SharedPreferences._setValue (package:shared_preferences/shared_preferences.dart:130:19)
E/flutter ( 8369): #2      SharedPreferences.setString (package:shared_preferences/shared_preferences.dart:116:7)
E/flutter ( 8369): #3      _LoginState.signIn (package:kiriapp/oten/login.dart:178:15)
E/flutter ( 8369): <asynchronous suspension>
E/flutter ( 8369):

这是我的代码,

signIn(String email, pass) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
      'email': email,
      'password': pass
    };
    var jsonResponse = null;
    var response = await http.post(Uri.parse("myapi"), body: data);
    if(response.statusCode == 200) {
      jsonResponse = json.decode(response.body);
      if(jsonResponse != null) {
        setState(() {
          _isLoading = false;
        });
        sharedPreferences.setString("token", jsonResponse['token']);
        Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => HomePage()), (Route<dynamic> route) => false);
      }
    }
    else {
      setState(() {
        _isLoading = false;
      });
      print(response.body);
    }
  }

谢谢

i am trying to login flutter authentication by viewing the video from https://www.youtube.com/watch?v=2DtFGF2v_vk&t=365s
but i get an error message

Reloaded 1 of 975 libraries in 954ms.
E/flutter ( 8369): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument(s) (value): Must not be null
E/flutter ( 8369): #0      ArgumentError.checkNotNull (dart:core/errors.dart:207:27)
E/flutter ( 8369): #1      SharedPreferences._setValue (package:shared_preferences/shared_preferences.dart:130:19)
E/flutter ( 8369): #2      SharedPreferences.setString (package:shared_preferences/shared_preferences.dart:116:7)
E/flutter ( 8369): #3      _LoginState.signIn (package:kiriapp/oten/login.dart:178:15)
E/flutter ( 8369): <asynchronous suspension>
E/flutter ( 8369):

here is my code

signIn(String email, pass) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    Map data = {
      'email': email,
      'password': pass
    };
    var jsonResponse = null;
    var response = await http.post(Uri.parse("myapi"), body: data);
    if(response.statusCode == 200) {
      jsonResponse = json.decode(response.body);
      if(jsonResponse != null) {
        setState(() {
          _isLoading = false;
        });
        sharedPreferences.setString("token", jsonResponse['token']);
        Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => HomePage()), (Route<dynamic> route) => false);
      }
    }
    else {
      setState(() {
        _isLoading = false;
      });
      print(response.body);
    }
  }

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2025-01-17 10:41:55

正如错误本身所说

未处理的异常:无效参数(值):不能为 null

ArgumentError.checkNotNull

SharedPreferences._setValue

似乎下面的行没有获得所需的值,并且可能 jsonResponse['token'] 是null

sharedPreferences.setString("token", jsonResponse['token']);

如果您没有尝试将 null 值设置为令牌键,请额外确定

As the error itself says

Unhandled Exception: Invalid argument(s) (value): Must not be null

ArgumentError.checkNotNull

SharedPreferences._setValue

Seems like the below line isn't getting the required values and probably jsonResponse['token'] is null

sharedPreferences.setString("token", jsonResponse['token']);

Be extra sure if you are not trying to set a null value to token key

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文