Skip to content

Commit

Permalink
docs: update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
noxasch committed Sep 24, 2022
1 parent d7e3e83 commit 8901532
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion app_widget/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ await appWidgetPlugin.getWidgetIds(
- To make sure this bug doesn't affect your widget udpate, you'll need to register
another task that longer maybe than your update widget task, and then cancel it
inside the callback.
- to avoid this use workmanager periodicTask instead

```dart
// Using workmanager chained OneOffTask
@pragma('vm:entry-point')
void onConfigureWidget(int widgetId) async {
final sharedPrefs = await SharedPreferences.getInstance();
Expand All @@ -337,7 +339,9 @@ void onConfigureWidget(int widgetId) async {
existingWorkPolicy: ExistingWorkPolicy.keep,
initialDelay: const Duration(minutes: 5),
);
// register a dumm task
// register a dummy task
// dummy task is required to fix flickering bug
// https://stackoverflow.com/questions/71603702/in-android-glance-widgets-are-flickering-during-every-update-even-if-there-i
await Workmanager().registerOneOffTask(
'DUMMY_TASK',
'dummyTask',
Expand All @@ -347,6 +351,30 @@ void onConfigureWidget(int widgetId) async {
);
}
// Using workmanager PeriodicTask
@pragma('vm:entry-point')
void onConfigureWidget(int widgetId) async {
final sharedPrefs = await SharedPreferences.getInstance();
await sharedPrefs.setInt('widget_id', widgetId);
// register task druing configure event in onConfigure callback
await Workmanager().registerPeriodicTask(
'$kUpdateWidgetTask-$widgetId',
kUpdateWidgetTask,
tag: kUpdateWidgetTag,
frequency: kWidgetUpdateIntervalDuration,
existingWorkPolicy: ExistingWorkPolicy.replace,
backoffPolicy: BackoffPolicy.exponential,
backoffPolicyDelay: const Duration(
seconds: 10,
),
initialDelay: const Duration(minutes: kWidgetUpdateIntervalInMinutes),
inputData: {
'widgetId': widgetId,
'payload': payload,
},
);
}
// in callbackDipatcher or some other file
final worksMapper = {'updateWidget': updateWidgetWorker};
Expand Down

0 comments on commit 8901532

Please sign in to comment.