【发布时间】:2020-06-02 01:27:06
【问题描述】:
我有一个让我讨厌抽屉的问题。我在 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();
}
【问题讨论】: