A.A
A.A

Reputation: 4091

Use Provider like a Factory

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?

Upvotes: 2

Views: 459

Answers (1)

Randal Schwartz
Randal Schwartz

Reputation: 44056

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

Upvotes: 3

Related Questions