将 Flutter 导航器状态重新加载到第一个屏幕

问题描述 投票:0回答:1

我想根据部门变更的 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 let me know how to do it. 
flutter dart navigation
1个回答
0
投票

可以使用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);
   
  }
© www.soinside.com 2019 - 2024. All rights reserved.