Flutter:防止抽屉始终在FutureBuilder中滑动时刷新

时间:2020-02-18 04:45:58

标签: flutter dart

我有一个问题让我讨厌抽屉。我在FutureBuilder中有抽屉,用于从API提取用户个人资料。问题是每当我滑动/打开抽屉时,它将在获取用户个人资料之前刷新。这会带来糟糕的用户体验。

我已经在搜索如何使FutureBuilder仅从this运行一次,但是抽屉仍然刷新。

我错过了什么?

自定义抽屉

class _DrysDrawerCustomState extends State<DrysDrawerCustom> {
  Future<List<UserModel>> userList;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final username = Provider.of<GlobalProvider>(context).username;
    userList = userApi.getUserByUsername(username: username);
  }

  @override
  Widget build(BuildContext context) {
    final globalProvider = Provider.of<GlobalProvider>(context, listen: false);
    //TODO Change Structure Drawer
    return FutureBuilder(
      future: userList,
      builder: (BuildContext context, AsyncSnapshot<List<UserModel>> snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return ErrorFutureBuilder(errorText: snapshot.error);
          } else {
            final user = snapshot.data[0];
            return DrawerCustom(
              imageUrl: "${userApi.baseImageUrl}/${user.fotoUser}",
              detailUser: [
                Text(user.namaUser),
                Text(user.namaGroup),
              ],
              drawerMenu: [
                DrawerMenu(
                  menuName: "Profil Info",
                  icon: Icons.people_outline,
                  onTap: () => Navigator.of(context)
                      .pushNamed(ProfilInfoScreen.routeNamed),
                ),
                DrawerMenu(
                  menuName: "Ganti Password",
                  icon: Icons.lock,
                  onTap: () => Navigator.of(context)
                      .pushNamed(ChangePasswordScreen.routeNamed),
                ),
                Divider(),
                DrawerMenu(
                  menuName: "Logout",
                  icon: Icons.arrow_back,
                  onTap: () {
                    globalProvider.removeUsername();
                    Navigator.of(context)
                        .pushReplacementNamed(LoginScreen.routeNamed);
                  },
                ),
              ],
            );
          }
        } else {
          return LoadingFutureBuilder();
        }
      },
    );
  }
}

全球提供商

GlobalProvider() {
    _initialSharedPreferences();
  }
String _username;
  String get username => _username;

  void _initialSharedPreferences() async {
    await getUsername();
  }

  Future updateUsername(String username) async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    await pref.setString("username", username);
    //! It's Important !!! After update / remove sharedpreferences  , must called getUsername() to updated the value.
    await getUsername();
    notifyListeners();
  }

  Future removeUsername() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    final result = await pref.remove("username");
    //! It's Important !!! After update / remove sharedpreferences  , must called getUsername() to updated the value.
    await getUsername();
    notifyListeners();
    return result;
  }

  Future getUsername() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    final result = pref.getString("username");
    _username = result;
    notifyListeners();
  }

1 个答案:

答案 0 :(得分:0)

使“用户”成为全局变量,并在内部构建中检查user是否为null,然后按照您的方式返回FutureBuilder(...);否则,只需立即返回DrawerCustom(...)。希望这能回答您的问题。

if(user == null)
  return FutureBuilder(...) // don't forget to set user as you've done already
else
  return DrawerCustom(...)