Search code examples
flutterdartaqueduct

sign in The method '_mulFromInteger' was called on null


I've got sign in methode in my provider.

  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
    }
  }

All works fine but when incorrect login credential are passed then I get misleading error

flutter: NoSuchMethodError: The method '_mulFromInteger' was called on null.
Receiver: null
Tried calling: _mulFromInteger(1000000)

The back end passing error code 400 and body {"error": "invalid client"} but I get that strange error as output. So what does that error means and why do I get that instead of body


Solution

  • As from @Suragch comments I had few problem in my code. First I thought that when server return 400 code then it will automatically throw an error and skip rest of the lines.. I was wrong so basically I had to uncomment my code for http exceptions and in my button catch the error

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