Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Deepakraj2022 opened this issue Jul 2, 2024 · 1 comment
Open

Comments

@Deepakraj2022
Copy link

Deepakraj2022 commented Jul 2, 2024

Hi 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


<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

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

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());
    }
}
@yahu1031
Copy link

yahu1031 commented Oct 7, 2024

Can you try running this verbose mode and share the app-package logs. @Deepakraj2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants