【发布时间】:2022-01-10 09:52:26
【问题描述】:
当我使用图标按钮调用 toggleFavorite 函数时,状态正在改变,但 ui 不会更新,但当我 热重载 时它会更新。我在stackoverflow上检查了一些类似的问题,但我无法弄清楚。
class ProductsNotyfier extends StateNotifier<List<Product>> {
ProductsNotyfier()
: super([
Product(
id: 'p1',
title: 'Red Shirt',
description: 'A red shirt - it is pretty red!',
price: 29.99,
imageUrl:
'https://images.unsplash.com/photo-1602810320073-1230c46d89d4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=687&q=80',
),
Product(
id: 'p2',
title: 'Trousers',
description: 'A nice pair of trousers.',
price: 59.99,
imageUrl:
'https://images.unsplash.com/photo-1603252110971-b8a57087be18?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80',
),
]);
List<Product> get items {
return [...state];
}
toggleFavorite(index) {
items[index].isFavotie = !items[index].isFavotie;
}
【问题讨论】:
标签: flutter dart flutter-dependencies riverpod flutter-state