'Flutter workmanger with sqlite
I'm building a finance app using flutter and flutter_drift for SQLite database and workmanger for background tasks.
Now I need a way to run an insert query in the background even if the app is closed every while like day/week/month.
I tried what below but throws an error that I have called AppDatabase class multiple time which is true as I can't get the database class instance from the main isolate using getIt
What I have done so far in code:
void callbackDispatcher() {
configureInjection(Environment.prod);
final ITransactionService transactionService = TransactionService(
TransactionRepository(TransactionsDao(AppDatabase())),
ContactPickerService(),
ImagePickerService(),
);
Workmanager().executeTask(
(task, inputData) async {
switch (task) {
case 'repeatedTransaction':
// final String tId = inputData['string'];
final String? id = inputData?['string'];
optionOf(id).fold(
() => null,
(id) async {
final Either<TransactionFailure, TransactionDomain?> t =
await transactionService.findTransactionById(id);
t.fold(
(l) => print('$l'),
(transactionDomain) {
if (transactionDomain != null) {
transactionService.create(transactionDomain.copyWith(
date: TransactionDate(DateTime.now())));
}
},
);
},
);
break;
default:
} //simpleTask will be emitted here.
return Future.value(true);
},
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
