Use Provider like a Factory

Viewed 206

Can I use Provider in flutter like a factory constructor? I want to watch, read and dispose my object

Currently to watch a value we can use context.watch<MyValue>(), I want to watch multiple instance of MyValue and I want to know can provider do it for me?

class MyValue{
   final int id;

   MyValue({this.id});

}

I need something like

final a = context.watch<MyValue>(id:1);
final b = context.watch<MyValue>(id:2);
final c = context.watch<MyValue>(id:3);
final d = context.watch<MyValue>(id:1);

assert(a==d);
assert(a!=b);

If Provider cannot do it, How can I implement it, or is there another library?

1 Answers

Take a look at Riverpod's .family. You can pass a parameter in just like that and create separate providers.

Related