Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

flutter怎样判断是第一次启动App?

试过SharedPredferences,但是flutter中如果value为空会直接报错
(作为对比,android里的sp可以设置一个默认值)
用什么办法判断比较好

ps 其实目的就是给SharedPredferences设置初始值,但是后期会涉及到数据库操作等等等等.....
现在用try-catch判断 出错返回默认值,想知道有没有什么更“优雅”一点的方式?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

没人回可太惨了
写一下自己的解决方法

///sp工具类
///preferences就是SharedPreferences
bool getBool(String key, bool defaultValue) {
  if (!preferences.containsKey(key))
    preferences.setBool(key, defaultValue);
 return preferences.getBool(key);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...