sign in The method '_mulFromInteger' was called on null

996

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);
  }
Share:
996
delmin
Author by

delmin

Updated on December 20, 2022

Comments

  • delmin
    delmin over 1 year

    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

    • Haidar
      Haidar about 4 years
      can you provide more details, where is the error happening, in the try or catch block?
    • delmin
      delmin about 4 years
      @LoVe it is in the catch block... commented in the code
    • Haidar
      Haidar about 4 years
      stackoverflow.com/questions/52549620/… may help clarify where the problem is coming from, also, try print(error) just to see if it can print something without giving an error
    • delmin
      delmin about 4 years
      @LoVe That link doesn't really help in this situation and print(error) prints the same error
    • Suragch
      Suragch about 4 years
      I would try to pear down the code until you isolate exactly where the error it getting thrown. Removing try/catch might help with that. I think the link that LoVe provided might actually be on the right track. There is a comment there about a null Duration causing such an error and you also have a Duration. Additionally, check the response status code before you try to do something with the response body. If the status code is 400 then there is no use trying to extract a non-existent access token.
    • Suragch
      Suragch about 4 years
      Side note: client apps probably don't need to worry about the userId. Let the server worry about that. The client only needs to worry about the email, password, and access token.
    • delmin
      delmin about 4 years
      @Suragch I've seen that comment and absolutely forgot about the Duration in my code.. Yes that might be the case of the issue. However I thought that try block will skip everything once response from http throw an error
    • Suragch
      Suragch about 4 years
      http doesn't throw an error just because the server returns a 400 or 500 code.
    • delmin
      delmin about 4 years
      @Suragch I see... to be honest I tried also to fix it as you can see in updated code (commented) but I get an error Unhandled Exception: Failed assertion: boolean expression must not be null So basically I can get rid of the try-catch block and simply use if else statement.. if response has error in the body throw error else continue
    • Suragch
      Suragch about 4 years
      I think what you meant to write is this: jsonResponse["error"] != null. I would still check response.statusCode rather than testing for null, though.
    • delmin
      delmin about 4 years
      @Suragch You're right. I just noticed that silly mistake ...All fixed and working as it should
    • Suragch
      Suragch about 4 years
      Great. Consider adding your own answer below so that others who come here can benefit.
    • delmin
      delmin about 4 years
      @Suragch Please you do it so I can at least give you some points for your help
    • Suragch
      Suragch about 4 years
      @delmin, I don't need the points. :) Besides, I prefer to answer questions with a single topic. This has two or three different issues.
    • delmin
      delmin about 4 years
      @Suragch you're right.. I'll do it then... Sorry for that :)