我目前没有 FutureProvider + shared_preferences 的实现:
class IdentityModel {
final bool isLoggedIn;
final int id;
final String userName;
final String avatarUrl;
const IdentityModel({
this.isLoggedIn = false,
this.id,
this.userName,
this.avatarUrl,
});
}
class IdentityNotifier extends StateNotifier<IdentityModel> {
IdentityNotifier() : super(_initialState);
static final _initialState = IdentityModel();
void loginAction(MyCustomUserClass user) {
state = IdentityModel(
isLoggedIn: true,
id: user.id,
userName: user.userName,
avatarUrl: user.avatarUrl,
);
}
void logoutAction() {
state = IdentityModel(
isLoggedIn: false,
id: null,
userName: null,
avatarUrl: null,
);
}
}
final identityProvider = StateNotifierProvider<IdentityNotifier>(
(ref) => IdentityNotifier(),
);
我想使用shared_preferences
包来保持状态,但我不太确定如何将我当前的实现转换FutureProvider
为用作获取/设置shared_preferences
用途的实现。async/await
我基本上想在应用程序启动时shared_preferences
使用它的方法来“加载”来初始化通知程序,然后调用通知程序方法中的方法和.get
shared_preferences
set
loginAction
logoutAction