Container-ന്റെ വ്യക്തമായ ഉടമസ്ഥതയോടെയുള്ള Flutter ഏകീകരണം.
ProviderScope-ന് container സ്വന്തമായി കൈവശം വയ്ക്കാം അല്ലെങ്കിൽ പുറത്തുനിന്ന് ഒന്ന് സ്വീകരിക്കാം. Consumer API-കൾ WidgetRef ആക്സസ് Riverpod-ശൈലി കോഡിനൊപ്പം പൊരുത്തപ്പെടുന്ന രീതിയിൽ നിലനിർത്തുന്നു.
ProviderScope ഉടമസ്ഥത
Container ഉടമസ്ഥത dispose ഉത്തരവാദിത്തം മാറ്റുന്നു.
ആന്തരിക container
ProviderScope(child: ...) സ്വയമേവ dispose ചെയ്യുന്നു
ബാഹ്യ container
ProviderScope(container: c, ...) ന് വിളിക്കുന്നവൻ c.dispose() ചെയ്യേണ്ടതാണ്
നിയന്ത്രണമില്ലാത്ത scope
UncontrolledProviderScope ഒരിക്കലും container dispose ചെയ്യില്ല
പൊതുവായ പിഴവ്
widget ടെസ്റ്റുകളിൽ, പുറത്ത് നിന്ന് inject ചെയ്ത ProviderContainer dispose ചെയ്യുന്നത് മറക്കരുത്; ഇല്ലെങ്കിൽ കാത്തിരിക്കുന്ന timer leak-ുകൾ ഉണ്ടാകാം.
Consumer വകഭേദങ്ങൾ
എല്ലാ ഓപ്ഷനുകളും WidgetRef പുറത്തുവിടുന്നു; widget ശൈലിയും local state ആവശ്യങ്ങളും അനുസരിച്ച് തിരഞ്ഞെടുക്കുക.
ആപ്പ് റൂട്ട്
ഓരോന്നും എപ്പോൾ ഉപയോഗിക്കണം
Consumer: ചെറിയ reactive മേഖലകൾക്കായുള്ള local builder block.
ConsumerWidget: build(context, ref) ഉള്ള stateless widget.
ConsumerStatefulWidget: ConsumerState-ൽ ref ഉള്ള stateful widget.
ഉദാഹരണം: ConsumerStatefulWidget
WidgetRef നും locale-ൽ മാറ്റാവുന്ന UI state നും രണ്ടും വേണമെങ്കിൽ ConsumerState ഉപയോഗിക്കുക.
class HomePage extends ConsumerStatefulWidget {
const HomePage({super.key});
@override
ConsumerState<HomePage> createState() => _HomePageState();
}
class _HomePageState extends ConsumerState<HomePage> {
bool expanded = false;
@override
Widget build(BuildContext context) {
final user = ref.watch(currentUser);
return Column(
children: [
Text('$user'),
Switch(
value: expanded,
onChanged: (v) => setState(() => expanded = v),
),
],
);
}
}
അടുത്ത ഘട്ടങ്ങൾ
ടെസ്റ്റിംഗ്
container ജീവിതചക്രം, overrides, async updates എന്നിവ unit, widget ടെസ്റ്റുകളിൽ പരിശോധിക്കുക.
ടെസ്റ്റിംഗ് തുറക്കുകAPI റഫറൻസ്
ProviderScope, WidgetRef, container methods എന്നിവയുടെ signatures കാണുക.
API റഫറൻസ് തുറക്കുക