Riverpod -Mixing AsyncValue with statenotifier failed to update UI

Viewed 344

I tried to process user input and update the localvalue in statenotifier which listens and its state is of AsyncValue. The problem is that, updating the value of local variable inside the statenotifier update the UI but updating the state variable never updates the UI, once data is fetched from repository, the UI never tries to fetch updated data from the repository(server).

/------Controller-------------

final purchaseOfAssetControllerProvider =
StateNotifierProvider<PurchaseOfAssetController, AsyncValue<Transaction>>(
    (ref) => PurchaseOfAssetController(ref.read)..init());

class PurchaseOfAssetController extends StateNotifier<AsyncValue<Transaction>> {
   final Reader _read;
   late Transaction initialState;
    String _assetName = '';
    String getAssetName() => _assetName;
    setAsset(LedgerMaster asset) {
     _assetName = asset.name;
   final data = state.data!.value;
   state = AsyncValue.data(
   data.copyWith(
    assetLedgerId: asset.id,
    particular: data.particular + '-' + asset.name,
  ),
  );
 }
PurchaseOfAssetController(this._read) : super(AsyncValue.loading());

  init() async {
    initialState = Transaction(
      amount: 0,
      particular: await _read(transactionTypeRepositoryProvider)
          .getTransactionTypeName(TransactionTypeIndex.PurchaseOfAssets),
      partyId: null,
      date: DateTime.now(),
    );

    _creditSideName = await _read(ledgerMasterRepositoryProvider)
        .getLedgerMasterName(initialState.creditSideLedgerId!);

    state = AsyncValue.data(
      initialState,
    );
    print(_creditSideName);
  }

As can be seen from the controller, the following lines update the local variable inside statenotifier, this updation reflects to the UI.

setAsset(LedgerMaster asset) {
     _assetName = asset.name;

But- the subsequent code update the state but it never rebuild the watching UI

state = AsyncValue.data(
   data.copyWith(
    assetLedgerId: asset.id,
    particular: data.particular + '-' + asset.name,
  ),
  );
   

---UI-------------------------

Widget build(BuildContext context, WidgetRef ref) {
AsyncValue<Transaction> currentState =
    ref.watch(purchaseOfAssetControllerProvider);
    
0 Answers
Related