我想根据部门变更的 if 条件将导航器状态重新加载到第一个屏幕。我的代码如下:
void didChangeDependencies() {
super.didChangeDependencies();
AuthenticationNotifier authenticationNotifier =
Provider.of<AuthenticationNotifier>(context, listen: true);
UserAppDepartmentModel? currentDepartment =
authenticationNotifier.getSelectedUserAppDepartment();
// Check if the Department has changed
if (currentDepartment != previousDepartment) {
setState(() {
cSelectedUserAppDepartment = currentDepartment;
previousDepartment = currentDepartment; // Update previous value
});
}
}```
please if let me know how to do it.
可以使用NavigatorKey解决。如下:
void didChangeDependencies() {
super.didChangeDependencies();
AuthenticationNotifier authenticationNotifier =
Provider.of<AuthenticationNotifier>(context, listen: true);
UserAppDepartmentModel? currentDepartment =
authenticationNotifier.getSelectedUserAppDepartment();
// Check if the Department has changed
if (currentDepartment != previousDepartment) {
setState(() {
cSelectedUserAppDepartment = currentDepartment;
previousDepartment = currentDepartment; // Update previous value
resetNavigators();
});
}
}
void resetNavigators() {
YourNavigatorKey.currentState?.popUntil((route) => route.isFirst);
}