Flutter - iOS in release mode reject network call

Viewed 54

I have a Flutter project able to send data over webservice, when app is not on foreground. Only on iOS release mode, Dio return an Unauthorized exception when app in on background when network call is performed. If app come on foreground, it works.

When app is on foreground on iOS, and foreground/background for Android there are no problems.

I'm using flutter_background_geolocation sdk.

bg.BackgroundGeolocation.onLocation((bg.Location location) async {
    bg.BackgroundGeolocation.startBackgroundTask().then((int taskId) {
      // Perform some long-running task
      api.postGeolocation(latitude: location.coords.latitude, longitude: location.coords.longitude).then((v) {
        // When long-running task is complete, signal completion of taskId.
        bg.BackgroundGeolocation.stopBackgroundTask(taskId);
      });
    });
  }, (bg.LocationError error) {
    logger.e('[onLocation] ERROR: $error');
  });

Api code :

Future<Resp> postGeolocation(Req request) async {
try {
  final json = jsonEncode(request.toMap());
  final response = await Dio().post(geolocationUrl,
      options: Options(headers: {
        HttpHeaders.contentTypeHeader: "application/json",
      }),
      data: json);

  if (response.statusCode == codeStatusOk && response.data != null) {
    return Resp(success: true, statusCode: response.statusCode, object: Resp.fromJson(response.data));
  }
  return Resp(success: false, statusCode: response.statusCode, error: errorMessageUnknown);
} on DioError catch (err) {
  return Resp(success: false, error: err.response?.statusMessage ?? errorMessageUnknown);
} on SocketException catch (err) {
  return Resp(success: false, error: errorMessageInternet);
}
}
0 Answers
Related