首页 > 解决方案 > Release APK behaves differently than debug with type Future is not a subtype of FutureOr error

问题描述

Im implementing a simple app with a login screen using bloc pattern. The app runs fine when running on debug mode on the android emulator or on my android phone from android studio. When I tried the release-apk with no changes on the code, I encounter the Future is not a subtype of FutureOr error.

Here's the method on the bloc with the logic:

 void submit() async{
    _loginStateController.sink.add(LoginStateLoading());

    User user = User();
    user.email = emailController.text;
    user.password = passwordController.text;

    String responseBody = await ApiClient.postUser(user, "/login").catchError( (error){
      developer.log("error_while_posting_user: " + error.toString());
      String errorMessage = error.toString();
      if(errorMessage == "missing email" || errorMessage == "missing password"){
        errorMessage = "Datos faltantes";
      }
      else if(errorMessage == "incorrect email or password"){
        errorMessage = "Correo o contraseña incorrecta";
      }
      else{
        errorMessage = "Error inesperado";
      }

      _loginStateController.sink.add( LoginStateError(errorMessage));
      return;
    });

    Map<String, dynamic> responseMap = json.decode(responseBody);

    Utils.saveLoginInfo(responseMap);

    _loginStateController.sink.add(LoginStateReady());

  }

Here's the code where the http request is being made:

static Future<String> postUser(User user, String path) async {
    Map<String, String> headers = Map();
    headers["content-type"] = "application/json";
    Map<String, dynamic> bodyMap = user.toJson();

    String body = jsonEncode(bodyMap);

    try {
      final response = await http.post(
          API_ENDPOINT + path, headers: headers, body: body).catchError( (error) {
        return Future.error(error);
      }).timeout(Duration(milliseconds: 10000));

      if (response == null) {
        return Future.error("request error");
      }

      if (response.statusCode != 200) {
        String errorMessage = jsonDecode(response.body)["message"];
        return Future.error(errorMessage);
      }

      return response.body;
    }

    catch (Exception) {
      return Future.error(Exception.toString());
    }

    return "";
  }

I've tried flutter clean several times and still getting the error.

标签: androidflutter

解决方案


have you checked if you have internet permission in the AndroidManifest.xml?

Can you try adding this line in the AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />


推荐阅读