Войдите метод '_mulFromInteger' был вызван на нуль - PullRequest
2 голосов
/ 30 апреля 2020

У меня есть метод входа в моем провайдере.

  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 и body {"error": "invalid client"}, но я получаю эту странную ошибку в качестве вывода. Так что же означает эта ошибка и почему я получаю ее вместо тела

1 Ответ

1 голос
/ 30 апреля 2020

Начиная с комментариев @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);
  }
...