River_pod: ProxyProviders in RiverPod

Created on 22 Sep 2020  路  1Comment  路  Source: rrousselGit/river_pod

I just recently migrated to Riverpod and I must say I'm liking it so far.
One thing i kind of miss from Provider is the following:

ChangeNotifierProxyProvider<MyModel, MyChangeNotifier>(
  create: (_) => MyChangeNotifier(),
  update: (_, myModel, myNotifier) => myNotifier
    ..update(myModel),
  child: ...
);

This creates a ChangeNotifier that has a reference of an other ChangeNotifier that gets updated through the update() method on the new ChangeNotifier. This means that the ChangeNotifier to be created, won't recompute when the referenced ChangeNotifier changes.

Atm i'm using this workaround for Riverpod:

Consumer(
        builder: (context, watch, child) {
          final firstProvider = watch(FirstModel.provider);
          final secondProvider = watch(SecondModel.provider);
          secondProvider.updateFirstModel(firstProvider)

          return ...

In this example the secondProvider would have an instance field called firstModel that gets updated through the method updateFirstModel().

enhancement

Most helpful comment

There's a WIP to allow something similar:

final myNotifier = ChangeNotifierProvider<MyModel>((ref) {
  final model = MyModel();

  ref.listen<Value>(provider, (Value value) {
    model.update(value);
  });

  return model;
});

>All comments

There's a WIP to allow something similar:

final myNotifier = ChangeNotifierProvider<MyModel>((ref) {
  final model = MyModel();

  ref.listen<Value>(provider, (Value value) {
    model.update(value);
  });

  return model;
});
Was this page helpful?
0 / 5 - 0 ratings