3

I use Provider with Workmanager

Workmanager is especially useful to run periodic tasks, such as fetching remote data on a regular basis.

I use Workmanager for get Notification in Background

but I need way when I get notification I need call function in Provider but i not have context

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    -->//// Code to get Notification from MY web get newNotificationData
     Provider.of<Auth>(context, listen: false).update_notification(newNotificationData);
    return Future.value(true);
  });
}

void main() {
  Workmanager().initialize(
    callbackDispatcher, // The top level function, aka callbackDispatcher
    isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
  );
  Workmanager().registerOneOffTask("1", "simpleTask"); //Android only (see below)
  runApp(MyApp());
}

Update I need to update current user Auth

    class Auth extends ChangeNotifier {
      bool _isLoggedIn = false;
      User? _user;
     void update_notification(newNotificationData){
       this._user!.notification.add(newNotificationData);
      }
      void Login(username,password){
       //// Code to login and get UserData
         this._isLoggedIn = true;
         this._user = User.fromJson(data);
      }
    }

User Model

class User {
  User(
      {required this.id,
       required this.username,
      required this.fullName,
      required this.email,
      required this.notification});
  User.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        username = json['username'],
        fullName = json['fullName'],
        email = json['email'],
        notification = json['notification'];
  final int id;
  final String username;
  final String fullName;
  final String email;

  List<dynamic> notification;

  Map<String, dynamic> toJson() => {
        'id': id,
        'username': username,
        'fullName': fullName,
        'email': email,
        'notification': notification,

      };
}
SAWA Group
  • 101
  • 1
  • 7
  • Please provide an [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – rckrd Sep 21 '21 at 21:10

1 Answers1

0

Create and keep the Auth-object where you can access it from both callbackDispatcher and the widget tree. For example using a singleton or something similar. Then in the flutter widget tree you can use Provider.value to expose Auth..

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    Auth.instance.update_notification(newNotificationData);
    return Future.value(true);
  });
}

void main() {
  Workmanager().initialize(
    callbackDispatcher,
    isInDebugMode: true
  );
  runApp(ChangeNotifierProvider.value(
         value: Auth.instance, // Same object as above
         child: MyApp()));
}
rckrd
  • 3,124
  • 1
  • 13
  • 23
  • I cannot access to current data (User instance) is null @rckrd – SAWA Group Sep 22 '21 at 09:22
  • So where’s `data` supposed to come from? This seems like an unrelated question to the original. – rckrd Sep 22 '21 at 19:43
  • Date user get from API and create new instance from user in provider ,and data notification is come from Pusher Broadcast, I get the data correctly but I need to update notification for current user in the provider I cannot access to current login user from callbackDispatcher – SAWA Group Sep 22 '21 at 20:34
  • You need to provide more context for me to understand. In your code above `user` in `Auth` will be null until `Login` is called. – rckrd Sep 23 '21 at 11:03