miniriverpod પાછળના design principles.

પેકેજ ઇરાદાપૂર્વક features ને સીમિત રાખે છે જેથી વર્તન સ્પષ્ટ રહે: args દ્વારા provider ઓળખ, scoped injection, અને અનુમાનિત disposal semantics.

Riverpod કરતા શું બદલાય છે

Generated family classes અને implicit notifier channels ના બદલે, miniriverpod subclass + args + explicit invoke ને પ્રાથમિકતા આપે છે.

Provider ઓળખ

runtimeType + args hash

family નો વિકલ્પ

Provider / AsyncProvider ની subclass બનાવો અને super.args((...)) પાસ કરો.

DI fallback

Scope<T>.required + overrideWithValue

આ કેમ મહત્વનું છે

સામાન્ય Dart constructors પરથી equality અને overrides વિશે વિચારી શકો છો, જે debugging અને tests ને સરળ રાખે છે.

args સાથે Provider ઓળખ

args provider key ને નિર્ધારિત કરે છે, એટલે સમાન args નો અર્થ ProviderContainer અંદર સમાન cache entry થાય છે.

ઓળખનો નિયમ

વ્યવહારિક પરિણામો

- અલગ family type જરૂરી નથી.
- દરેક argument માટે override provider instances બનાવીને થાય છે.
- અનુમાનિત caching માટે args સ્થિર અને immutable રાખો.

ઉદાહરણ: family જેવો provider + Scope fallback

Constructor argument ને ઓળખ તરીકે ઉપયોગ કરો અને Scope મારફતે fallback instance inject કરો.

class ProductProvider extends AsyncProvider<List<Product>> {
  ProductProvider({this.search = ''}) : super.args((search,));
  final String search;

  static final fallback = Scope<ProductProvider>.required('product.fallback');

  @override
  FutureOr<List<Product>> build(ref) async {
    final api = ref.watch(productsApiProvider);
    return api.search(q: search);
  }
}

// Inject
ProviderScope(
  overrides: [
    ProductProvider.fallback.overrideWithValue(ProductProvider(search: 'jeans')),
  ],
  child: const App(),
);
Scope dependency wiring ને સ્પષ્ટ અને ટેસ્ટ-ફ્રેન્ડલી રાખે છે.
overrideWithValue દરેક provider instance પર કામ કરે છે, જેમાં args આધારિત instances પણ સામેલ છે.
autoDispose નું વર્તન subclass + args વાપરવાથી બદલાતું નથી.

આગલા પગલાં

Providers અને વાંચન

watch/read/listen અને AsyncProvider.future માટે ચોક્કસ patterns જુઓ.

Provider ખોલો

Mutations

mutation tokens, mutate અને ref.invoke સાથે state updates અમલમાં મૂકો.

Mutations ખોલો