'Update View when its data changed from another ViewModel

I have the following issue. It's my HistoryViewModel class that I use for HistoryScreen

class HistoryViewModel extends ChangeNotifier {
  final _databaseService = HistoryDatabaseService();
  var _historyList = <History>[];
  List<History> get historyList => List.of(_historyList);

  HistoryViewModel() {
    getHistoryList();
  }

  void updateState(){
    notifyListeners();
  }

  Future<void> getHistoryList() async {
    try {
      final historyFromDb = await _databaseService.getHistoryList();
      _historyList = historyFromDb;
      notifyListeners();
    } catch (e) {
      print(e);
    }
  }
}

And in History Screen there is List "historyList" that contains objects that I get from SQFLite. I use them to build widgets with ListViewBuilder

class ListOfOperationsWidget extends StatelessWidget {
  const ListOfOperationsWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    final historyList = context.watch<HistoryViewModel>().historyList;

    return Container(
      decoration: BoxDecoration(
          color: background, borderRadius: BorderRadius.circular(23)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(left: 25.w, right: 25.w, top: 20.h),
            child: Text(
              "List of operations",
              style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.w500),
            ),
          ),
          Container(
            height: 285.h,
            width: MediaQuery.of(context).size.width,
            child: ListView.builder(
                // physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                itemCount: historyList.length,
                itemBuilder: (BuildContext context, int index) {

Problem is that I need to add new data to History database table from another ViewModel.

class ExpenseViewModel extends ChangeNotifier
    implements DatePickerAbstractViewModel {
  final _databaseService = ExpenseDatabaseService();
  final _historyDatabaseService = HistoryDatabaseService();

  var currentExpense = Expense.empty();
  var operationDate = DateTime.now();
  final _historyViewModel = HistoryViewModel();

  Future<void> saveExpense() async {
    try {
      _historyDatabaseService.addToHistory(History(
          operationTitle: currentExpense.title,
          operationAmount: accountToExpenseAmount,
          operationType: receivedAccount.title,
          iconIndex: currentExpense.iconIndex,
          colorIndex: currentExpense.colorIndex,
          operationDate: operationDate.toString()));
      _historyViewModel.getHistoryList();
      _historyViewModel.updateState();
      notifyListeners();
    } catch (e) {
      print(e);
    }
  }

}

So when I add new data to History database table I want to rebuild HistoryScreen with Provider but can't understand how?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source