Skip to content

Possible to access static references if app running? #198

Closed
@BestAndroidTeam

Description

@BestAndroidTeam

I created a work that fetch data from internet, then updates a moor database.
Meanwhile, my app listen to this database's changes (with streams) to update the ui.
However I cannot access the Database client instance, which is a Singleton, from the WorkManager (it is null), so I have to create a new client instance.
So when I update the Database via this new client instance, my UI is not notified of the changes, as it listens to an other instance.

Ex:

class Database {
  static Database _instance;
  static Database get instance => _instance ??= Database();

  Database() {
    print('Database instance is null, creating new');
  }
}

class App {
  static void start() async {
    WidgetsFlutterBinding.ensureInitialized();

    // Initialize workmanager
    Workmanager.initialize(
      WorkManager.callbackDispatcher,
      isInDebugMode: AppConstants.MODE_DEBUG,
    );
    
    Database.instance; // prints 'Database instance is null, creating new'
    Database.instance; // prints Nothing as the Database client has already been created

    await Workmanager.registerOneOffTask(
      'myUniqueName',
      'myTaskName',
      inputData: {},
    );

    // Start app
    final app = App();
    runApp(Container());
  }
}

void callbackDispatcher() {
  Workmanager.executeTask((taskName, inputData) async {
    Database.instance; // prints 'Database instance is null, creating new'
  }
}

With Android's WorkManager, I could access static instances and it worked fine. Would it be possible to do the same with flutter_workmanager?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions