我正在尝试使用Flutter Provider程序包来管理应用程序状态,但是我认为我缺少一些东西。 Hive用于将用户信息存储在这样的对象中:
background-color
这是应用程序流程: 用户位于 addresses.dart (StatefulWidget)中,该列表显示用户添加的当前地址的列表。如果单击了地址,则其 Navigator.pushNamed 到 edit_address.dart (StatefulWidget)(使用Navigator参数传递地址ID),则将检索Address对象和信息准备好了。如果用户单击按钮,它将使用UserViewModel删除地址,该方法如下所示:
class User {
String id;
List<Address> addresses;
}
注意::Navigation.goBack()是Navigator.pop()的简单包装
如果您注意到了,我有 notifyListeners()方法,该方法必须更新 addresses.dart
这是 addresses.dart
Future deleteAddress(String addressId) async {
if(UserStorage.singleton.getUserData().addresses.length <= 1) {
Snackbars.showFailed(message: "No puedes eliminar todas tus direcciones.");
return;
}
Snackbars.showLoading(message: "Eliminando...");
ApiStatusObj<AddressDto> status = await ReddiClient.accountApi.deleteAddress(addressId);
if(status.isSuccess) {
_userData.addresses.remove(status.data);
await UserStorage.singleton.setNewUserDetails(_userData);
Navigation.goBack();
Navigation.goBack(); // I'm using two goBack because I have to dismiss the previusly opened snackbar.
notifyListeners();
Snackbars.showFrom(status, "La dirección se eliminó con éxito.");
}
}
我要监听的属性是:
class _StreetsPageState extends State<StreetsPage> {
@override
Widget build(BuildContext context) {
return Consumer<CustomerViewModel>(
builder: (context, model, child) {
return ScaffoldWrapper(
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
Navigation.navigate(RouteConfiguration.createStreetPage);
},
label: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("AÑADIR", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
SizedBox(width: 10),
Icon(Icons.add, color: Colors.white)
],
),
),
title: "Mis direcciones",
child: Container(
child: Card(
child: SingleChildScrollView(
child: Column(
children: getStreetsWidgets(model.userAddress),
),
),
),
),
);
},
);
}
List<Widget> getStreetsWidgets(List<AddressDto> addresses) {
List<Widget> widgets = List<Widget>();
for(AddressDto address in addresses) {
String addressString = formatStreetAddress(address);
widgets.add(ListTile(
title: Text(addressString),
subtitle: Text("${address.city}, ${address.region}"),
trailing: IconButton(
icon: Icon(Icons.edit),
onPressed: () {
Navigation.navigate(RouteConfiguration.editStreetPage, arguments: address.id);
},
),
));
}
return widgets;
}
}
我也尝试过:
List<AddressDto> userAddress = UserStorage.singleton.getUserData().addresses;
是的,由于我在 getStreetsWidgets 中放置了一个断点并对其进行了调用,因此重新构建了UI,但是当我从 edit_address.dart 中弹出因为如果删除了该地址,它应该会消失),UI仍然显示从存储地址中删除的地址。为什么我想念?
更新:要查看 addresses.dart 中的更改,我必须从该页面弹出,然后再次将 Navigator.pushNamed 更改为 addresses.dart