【问题标题】:write and read SharedPreferences flutter写和读 SharedPreferences 颤动
【发布时间】:2020-04-11 20:27:12
【问题描述】:

从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 (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")

【问题讨论】:

    标签: flutter sharedpreferences


    【解决方案1】:

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

     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)));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-17
      • 2019-07-09
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多