You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What's the best practice for data updates in Riverpod: AsyncNotifier vs direct provider?
Current Setup
I have this widget that uses a provider to fetch data:
classTeacherSalaryDtlextendsHookConsumerWidget {
constTeacherSalaryDtl({super.key});
@overrideWidgetbuild(BuildContext context, WidgetRef ref) {
final provider =teacherSalaryDtlNotifierProvider(
SelectSalaryDtlRequest(
teacherId: teacherId,
calcYm: calcYm,
),
);
final notifier = ref.read(provider.notifier);
final state = ref.watch(provider);
returnLayoutBuilder(
builder: (context, constraints) {
return state.when(
data: (data) {
returnColumn(
children: [
// Display the dataText(data.salaryAmount.toString()),
// Input field to update salaryTextField(
keyboardType:TextInputType.number,
decoration:InputDecoration(labelText:'New Salary'),
// ❓ How do I update data when this value changes?// onChanged: (value) {// // What do I do here to update the data?// },
),
// ❓ How do I save the updated value?ElevatedButton(
onPressed: () {
// What do I call here?
},
child:Text('Save'),
),
],
);
},
loading: () =>CircularProgressIndicator(),
error: (error, stack) =>Text('Error: $error'),
);
},
);
}
}
The Dilemma
If I use the code like above, I can't properly use the notifier for MVVM pattern.
If I try to create a proper notifier, it ends up looking like this:
@riverpodclassTeacherSalaryDtlNotifierextends_$TeacherSalaryDtlNotifier {
@overrideFuture<SelectSalaryDtlResponse> build(SelectSalaryDtlRequest request) async {
final response =await ref.read(selectSalaryDtlProvider(request).future);
return response;
}
// All API error handling has to be done here// But the pattern seems strange
}
Question
What's the best practice here?
Should I use the notifier approach despite it feeling awkward for updates?
Is there a better pattern for handling data modifications in Riverpod?
How do you implement proper MVVM with Riverpod's AsyncNotifiers?
I can't find clear examples that demonstrate a clean architecture approach with Riverpod for both reading AND updating data.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
What's the best practice for data updates in Riverpod: AsyncNotifier vs direct provider?
Current Setup
I have this widget that uses a provider to fetch data:
The Dilemma
If I use the code like above, I can't properly use the notifier for MVVM pattern.
If I try to create a proper notifier, it ends up looking like this:
Question
What's the best practice here?
I can't find clear examples that demonstrate a clean architecture approach with Riverpod for both reading AND updating data.
Beta Was this translation helpful? Give feedback.
All reactions