miniriverpod ਦੇ ਪਿੱਛੇ ਡਿਜ਼ਾਈਨ ਸਿਧਾਂਤ।
ਇਹ ਪੈਕੇਜ ਜਾਣਬੁੱਝ ਕੇ ਵਿਸ਼ੇਸ਼ਤਾਵਾਂ ਨੂੰ ਸੀਮਿਤ ਕਰਦਾ ਹੈ ਤਾਂ ਜੋ ਵਿਹਾਰ ਸਪਸ਼ਟ ਰਹੇ: args ਰਾਹੀਂ provider identity, scoped injection, ਅਤੇ ਅਨੁਮਾਨਯੋਗ disposal semantics।
Riverpod ਤੋਂ ਕੀ ਬਦਲਦਾ ਹੈ
Generated family classes ਅਤੇ implicit notifier channels ਦੀ ਬਜਾਏ, miniriverpod subclass + args + explicit invoke ਨੂੰ ਤਰਜੀਹ ਦਿੰਦਾ ਹੈ।
Provider identity
runtimeType + args hash
family alternative
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 ਹੈ।
Identity rule
ਵਿਹਾਰਿਕ ਨਤੀਜੇ
- ਵੱਖਰਾ family type ਲੋੜੀਂਦਾ ਨਹੀਂ ਹੈ.
- ਹਰ argument ਲਈ override provider instances ਬਣਾਕੇ ਕੀਤਾ ਜਾਂਦਾ ਹੈ.
- ਅਨੁਮਾਨਯੋਗ caching ਲਈ args ਨੂੰ ਸਥਿਰ ਅਤੇ ਅਪਰਿਵਰਤਨਸ਼ੀਲ ਰੱਖੋ.
ਉਦਾਹਰਨ: family-ਜਿਹਾ provider + Scope fallback
Constructor argument ਨੂੰ identity ਵਜੋਂ ਵਰਤੋ ਅਤੇ 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(),
);
ਅਗਲੇ ਕਦਮ
ਪ੍ਰੋਵਾਈਡਰ ਅਤੇ ਪੜ੍ਹਨ
watch/read/listen ਅਤੇ AsyncProvider.future ਲਈ ਠੋਸ patterns ਵੇਖੋ.
ਪ੍ਰੋਵਾਈਡਰਾਂ ਨੂੰ ਖੋਲ੍ਹੋ