Use Provider like a Factory

169

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

Share:
169
WebMaster
Author by

WebMaster

I'm a fullstack developer

Updated on December 25, 2022

Comments

  • WebMaster
    WebMaster over 1 year

    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?