繁体   English   中英

写入和读取 SharedPreferences flutter

[英]write and read SharedPreferences flutter

从 url 读取 txt 文件

  var responser = await http.get("https://xxx .com/api.txt");
    var puu = responser.body;
    int iss = int.parse(puu);

并尝试获取上次保存的内容

  SharedPreferences prefs;
  String userName = prefs.getString("counter");

然后使用 if saved before 不要做任何事情,如果之前没有保存新值则需要运行它

if (userName == iss) {
} else {
  prefs.setInt('counter', iss);
  Category category = Category();
  category.id = puu;

  Navigator.of(context).push(MaterialPageRoute(
      builder: (BuildContext context) => FourthRoute(
          category: category,
          accountLogin: accountLogin,
          account: currentAccountProfile)));
}

得到错误

 Unhandled Exception: NoSuchMethodError: The method 'getString' was called on null.
Receiver: null
Tried calling: getString("counter")

我修好了....希望对任何人都有帮助

 SharedPreferences prefs = await SharedPreferences.getInstance();
    var responser = await http.get("https://xxxx .com/api.txt");
    var puu = responser.body;
    String puuusuread = prefs.getString('puuusu');

    print(puu);
    print(puuusuread);

    if (puu == puuusuread) {
    } else {
      prefs.setString('puuusu', puu);
      Category category = Category();
      category.id = puu;
      Navigator.of(context).push(MaterialPageRoute(
          builder: (BuildContext context) => FourthRoute(
              category: category,
              accountLogin: accountLogin,
              account: currentAccountProfile)));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM