user2664139
user2664139

Reputation: 21

Work Manager Thread crashes when API is called from Http or Dio

I am developing an app which should fetch and upload data using Dio. When I use Dio.Get() or Dio.Push(), workmanager thread is closed abruptly. I tried to catch the exception but I am unable catch any exception. I am adding code below

AndroidManifest

Changes done in my android manifest

<receiver
    android:name="be.tramckrijte.workmanager.BackgroundWorker"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.REBOOT" />
        <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
         <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

<service
            android:name="id.flutter.flutter_background_service.BackgroundService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="true"
            android:foregroundServiceType="dataSync">
  </service>

main.dart

I have initialised the work manager from the main.dart

void main() async {
.....
  Workmanager().initialize( _callbackDispatcher_,
      isInDebugMode: true 
      );
....
}
void callbackDispatcher() {

try {
   Workmanager().executeTask((task, inputData) async {
     switch (task) {
       case 'simpleTask':
         print('Simple Task executed');
         break;
       case 'uploadJob':
         await  WorkManagerJob().GetExportSetting(inputData);
         break;
       // Handle other tasks
     }
     return Future.value(true);
   });
 } catch (exce) {
   print(exce);
 }
}

WorkManagerJob.Dart

This is Dart where I make APi call. Here I have mentioned only the api call made for getting data

GetExportSetting(Map<String, dynamic>? data)
{
 int inspectionKey = data!['inspectionKey'];
    Global.currentLogin = LoginModel();
    Global.currentLogin!.token = data['token'];
    Global.currentLogin!.account = data['account'];
    Global.currentLogin!.server = data['server'];

final url = Uri.parse(Global.apiUrl + "api/GetExportSettings");
    try {
      final response = await http.get(
        url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
          '"Access-Control-Allow-Origin"': "*",
          'x-functions-key': Global.functionKey,
          'AccountAlias': Global.currentLogin!.account!,
          'token': Global.currentLogin!.token!,
        },
      );

      if (response.statusCode == 200) {
        final jsonResponse = jsonDecode(response.body);
      } else {}
    } catch (e) {
      print(e.toString());
    }
}

I was able to debug till the API call was made. But once it is call is executed, I lose debug point and thread crashes. I dont the get desired output.

Upvotes: 0

Views: 82

Answers (0)

Related Questions