I have a TaskEditor
screen and a TaskEditorController
which extends Notifier
. State is a Task
model. I want to make the default value of TaskEditorController
as the Task
I get as navigation params from another screen. To achieve that, this is what I am doing. Though this is working fine, I am little concerned over the practice.
class TaskEditorController extends Notifier<Task> {
TaskEditorController(this._initialTask);
final Task _initialTask;
@override
Task build() {
return _initialTask.copyWith();
}
}
final taskEditControllerProvider = NotifierProvider.autoDispose<TaskEditorController, Task>(
(() => throw UnimplementedError()));
And this how I am overriding it.
class TaskEditor extends HookConsumerWidget {
const TaskEditor(this._task);
final Task _task;
@override
Widget build(BuildContext context, WidgetRef ref) {
return ProviderScope(
overrides: [
taskEditControllerProvider
.overrideWith(() => TaskEditorController(_task))
],
This is "alright". There's nothing inherently wrong with doing this.
But this approach is a bit discouraged. Relying on overriding providers within the widget tree is considered as "advanced" and "should be avoided if possible".
Chances are there's a different solution to your problem which does not involve such practice.