Home > Software engineering >  Future.then must return a value of the returned future's type
Future.then must return a value of the returned future's type

Time:03-24

I'm continuously getting this error

Unhandled Exception: Invalid argument(s) (onError): The error handler of Future.then must return a value of the returned future's type

NetworkApi

class NetworkApi {
  NetworkApi(this.url);

  final String url;

  Future getData() async {
    http.Response response = await http.get(Uri.parse(url));

    if (response.statusCode == 200) {
      String data = response.body;

      return jsonDecode(data);
    } else {
      if (kDebugMode) {
        print(response.statusCode);
      }
    }
  }
}

ApiSelector

class ApiSelector {
  Future getApiData({String categoryName = ""}) async {
    late NetworkApi _api;
    if(categoryName != ""){
     _api = NetworkApi('api');
    }else{
      _api = NetworkApi('myapi');
    }
    var Data = await _api.getData();
    if (Data['status'] == 'error') {
      throw'Error while fetching data';
    }
    return MyModel.fromJson(Data);
  }
}

MyFunction To Call API selector

  Future getDataFromApi() async {
    await _apiSelector
        .getApiData(categoryName: "Some Category")
        .then((value) {
      myModel = value as MyModel;
      print(myModel);
      return value;
    } ,onError: (error) {
     print(error);
      return error;
    },);
  }

I have tried everything to solve this issue. May you please guide me what to do. I have even tried with exception handling but still getting this error.

CodePudding user response:

Specify the Future of return type like Future<dynamic>

NetworkApi

class NetworkApi {
  NetworkApi(this.url);

  final String url;

  Future<dynamic> getData() async {
    http.Response response = await http.get(Uri.parse(url));

    if (response.statusCode == 200) {
      String data = response.body;

      return jsonDecode(data);
    } else {
      if (kDebugMode) {
        print(response.statusCode);
      }
    }
  }
}

ApiSelector

class ApiSelector {
  Future<MyModel> getApiData({String categoryName = ""}) async {
    late NetworkApi _api;
    if(categoryName != ""){
     _api = NetworkApi('api');
    }else{
      _api = NetworkApi('myapi');
    }
    var Data = await _api.getData();
    if (Data['status'] == 'error') {
      throw'Error while fetching data';
    }
    return MyModel.fromJson(Data);
  }
}

MyFunction To Call API selector

  Future<MyModel?> getDataFromApi() async {
   try {
     var value = await _apiSelector.getApiData(categoryName: "Some Category");
     myModel = value as MyModel;
     return myModel;

   } catch(error) {
      print(error);
      return error;
   }
  }

CodePudding user response:

class ApiSelector {
  Future<MyModel> getApiData({String categoryName = ""}) async {
    late NetworkApi _api;
    if(categoryName != ""){
     _api = NetworkApi('api');
    }else{
      _api = NetworkApi('myapi');
    }
    var Data = await _api.getData();
    if (Data['status'] == 'error') {

    //I had to replace throw with Future.value Now All Errors are gone.
             return Future.value( 'Error while fetching data');
    }
    return MyModel.fromJson(Data);
  }
}

I hope this will also help others who will face this kind of error sometime in future.

  • Related