Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpUtil 在第一个页面可以正常存取,跳转页面_prefs后为null #37

Open
liyunfu opened this issue Oct 15, 2020 · 9 comments

Comments

@liyunfu
Copy link

liyunfu commented Oct 15, 2020

No description provided.

@liyunfu
Copy link
Author

liyunfu commented Oct 15, 2020

1.在登录页面存下用户信息,
2.然后跳转到首页可以正常获取,
3.再从首页跳转到用户详情页,_prefs为null

@liyunfu
Copy link
Author

liyunfu commented Oct 15, 2020

回到首页,仍然可以正常获取

@Sky24n
Copy link
Owner

Sky24n commented Oct 15, 2020

发下代码吧

:octocat: From gitme Android

@liyunfu
Copy link
Author

liyunfu commented Oct 15, 2020

初始化部分
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SpUtil.getInstance();

登录成功保存
SpUtil.putString(SPKey.USER_INFO, json.encode(data));

跳转首页
Navigator.pushReplacementNamed(context, RouteUtil.INDEX_PAGE);

首页正常读取
@OverRide
void initState() {
super.initState();

_userStr = SpUtil.getString(SPKey.USER_INFO);

_userInfo = JsonUtil.getObj(_userStr, (v) => UserInfo.fromJson(v));

}

跳转个人中心
Navigator.pushNamed(context, RouteUtil.USER_INFO_PAGE)

个人中心读取
@OverRide
void initState() {
super.initState();
String userStr = SpUtil.getString(SPKey.USER_INFO);

}

@liyunfu
Copy link
Author

liyunfu commented Oct 15, 2020

没有做什么特殊的操作,读取的方式也一样的

@Sky24n
Copy link
Owner

Sky24n commented Oct 15, 2020

你发个简单的demo到我邮箱,[email protected]

:octocat: From gitme Android

@Sky24n
Copy link
Owner

Sky24n commented Oct 15, 2020

另外SpUtil是支持存取Object的。
SpUtil.putObject("loc_city", city);
City hisCity = SpUtil.getObj("loc_city", (v) => City.fromJson(v));

@liyunfu
Copy link
Author

liyunfu commented Oct 16, 2020

刚才demo写好了,但是没有出问题,我又检查了一下我的项目。发现原因是因为一键导包的时候ide 自己分成了两种方式导包。
import 'file:///D:/FlutterProjects/flutter_test/lib/common/sp_util.dart';
import 'package:flutter_test/utils/sp_util.dart';

@liyunfu
Copy link
Author

liyunfu commented Oct 16, 2020

还是非常感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants