I writing an app using flutter that add item to list (using simpleDialog) and show thus items in ListView when you add on.
The problem is that the list only update when I move to another page and them get to the list page, so the data is there. I just don’t understand how to make the list to re-render…
This is the provider I declare
@riverpod
List<Task> tasks(TasksRef ref) => [];
This is my widget how preset the list (tasks)
class TasksScreen extends StatefulHookConsumerWidget {
const TasksScreen({super.key});
@override
ConsumerState<TasksScreen> createState() => _TasksScreenState();
}
class _TasksScreenState extends ConsumerState<TasksScreen> {
@override
Widget build(BuildContext context) {
List<model.Task> tasks = ref.read(tasksProvider);
List<Task> list = [];
list = [...test(tasks)];
return ProviderScope(
child: Scaffold(
body: ListView.builder(
itemBuilder: (context, index) {
return Task(task: ref.read(tasksProvider)[index]);
},
itemCount: ref.read(tasksProvider).length,
),
));
}
List<Task> test(List<model.Task> list) {
List<Task> tasks = [];
for (var i = 0; i < list.length; i++) {
tasks.add(Task(task: list[i]));
}
return tasks;
}
}
Thanks a lot for helpers !
I expecting that the changing of the provider lead to the list that render the page and show the list
2
Answers
Sorry, i have never used the riverpod package before, so i can’t directly give you advice on this and you might have to wait for a different answer.
But for the build method where you want to trigger rebuilds, you should use
watch
instead ofread
.And i don’t know if riverpod also has some wrapper, or helper class like a
ChangeNotifier
that you need to wrap your list in.Converted to the default provider package, a working example for your code snippet would look like:
Maybe this could still help you. I would recommend looking at the documentation of the riverpod package.
You should use
watch
instead of read sinceread
does not rebuild the widget when the state change as describe in the document