Closed
Description
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
Labels
No labels