Mikel Tawfik
Mikel Tawfik

Reputation: 746

write and read SharedPreferences flutter

read txt file from url

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

and try to get what did saved last time

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

then use if saved before dont do any thing , need to run it if new value not 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)));
}

get error

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

Upvotes: 0

Views: 36

Answers (1)

Mikel Tawfik
Mikel Tawfik

Reputation: 746

i fix it .... hope that's help any one

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

Upvotes: 0

Related Questions