Skip to content

Instantly share code, notes, and snippets.

@rrousselGit
Created July 2, 2020 04:26
Show Gist options
  • Save rrousselGit/ddd92ae211bee94b0188ddec4e01e2cd to your computer and use it in GitHub Desktop.
Save rrousselGit/ddd92ae211bee94b0188ddec4e01e2cd to your computer and use it in GitHub Desktop.
reading a family without the key
final todosFamily = ProviderFamily<Todo, int>((ref, id) {
return Todo(
id: '$id',
description: 'Todo $id',
);
});
final currentTodo = Provider<Todo>((ref) => null);
class List extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return ProviderScope(
overrides: [
currentTodo.overrideAs(
Provider((ref) => ref.read(todosFamily(index))),
),
],
child: Item(),
);
},
);
}
}
class Item extends HookWidget {
@override
Widget build(BuildContext context) {
final todo = useProvider(currentTodo);
return Text(todo.description);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment