I have a list items in provider model, and i want to set a observer listener to it when i add or remove item, while update part of related UI in the stateless widget (not with setState in Stateful widget).
// Provider
class AppState extends ChangeNotifier {
List<Item> _items;
List<Item> get items => _items;
Future<void> addItem(...) async {
...
notifyListeners();
}
Future<void> deleteItem(...) async {
...
notifyListeners();
}
}
// UI
class _itemList extends StatelessWidget {
@override
Widget build(BuildContext context) {
var appState = context.watch<AppState>(); // If I watch the list, it will rebuild all of it.
return ListView.builder(
itemCount: appState.items.length,
itemBuilder: (context, index) {
final item = appState.items[index];
return ListTile( // How to observer the item of list instead to rebuild all of it?
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(item.name),
Icon(Icons.keyboard_arrow_right, color: Colors.grey)
],
)
});
}
}