miniriverpod వెనుక ఉన్న డిజైన్ సూత్రాలు.
ఈ ప్యాకేజ్ ప్రవర్తన స్పష్టంగా ఉండేందుకు ఉద్దేశపూర్వకంగా ఫీచర్లను తగ్గిస్తుంది: args ద్వారా provider గుర్తింపు, 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 ను స్థిరంగా, immutable గా ఉంచండి.
ఉదాహరణ: 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 కోసం స్పష్టమైన నమూనాలు చూడండి.
ప్రొవైడర్లను తెరవండిమ్యూటేషన్లు
mutation tokens, mutate, మరియు ref.invoke తో state updates ను అమలు చేయండి.
మ్యూటేషన్లను తెరవండి