Home > Back-end >  Unhandled Exception: Invalid argument(s) (value): Must not be null
Unhandled Exception: Invalid argument(s) (value): Must not be null

Time:03-04

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

CodePudding user response:

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

  • Related