Sunday, November 8, 2026
HomeiOS Developmentandroid - How one can take away outdated worth and replace with...

android – How one can take away outdated worth and replace with a brand new one utilizing SharedPreferences flutter

[ad_1]

I’m making a trivia app, I’ve three perform that load the counter, second one increment the counter with a rating worth and the third unload the counter. load and increment features are referred to as in initState in order that the rating is incremented and exhibited to the consumer on one other display. Nonetheless, I’m having issue eradicating the outdated rating from the counter and replace it with a brand new rating when the consumer restarts the sport. Need assistance, Thanks

beneath is the code.

1 display


class ResultScreen extends StatefulWidget {
  int rating;

  // static Future init() async {
  //   prefs = await SharedPreferences.getInstance();
  // }

  ResultScreen(this.rating, {Key? key}) : tremendous(key: key);

  @override
  _ResultScreenState createState() => _ResultScreenState();
}

class _ResultScreenState extends State<ResultScreen> {
  int score1 = 0;
  // String _haveStarted = '';
  // bool Restart = false;

  void loadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      score1 = (prefs.getInt('counter') ?? 0);
    });
  }

  void unloadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.take away('counter');
  }

  void _incrementCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    setState(() {
      score1 = ((prefs.getInt('counter') ?? 0) + widget.rating);
      prefs.setInt('counter', score1);
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    tremendous.initState();
    loadCounter();
    _incrementCounter();
  }

2nd display code to show consequence

  const cardView1({
    Key? key,
  }) : tremendous(key: key);

  @override
  State<cardView1> createState() => _cardView1State();
}

class _cardView1State extends State<cardView1> {
  int score1 = 0;

  void loadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      score1 = (prefs.getInt('counter') ?? 0);
    });
  }

  // void _incrementCounter() async {
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   setState(() {
  //     score1 = ((prefs.getInt('counter') ?? 0) + widget.rating);
  //     prefs.setInt('counter', score1);
  //   });
  // }

  void unloadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.take away('counter');
  }

  @override
  void initState() {
    tremendous.initState();
    loadCounter();
    // unloadCounter();

    ///no matter you need to run on web page construct
  }

  @override
  Widget construct(BuildContext context) {
    return Container(
      width: 250,
      // (MediaQuery.of(context).measurement.width - 90) / 2,
      top: 170,
      margin: EdgeInsets.solely(
        left: 15,
        backside: 15,
        prime: 15,
      ),
      padding: EdgeInsets.solely(backside: 5),
      ornament: BoxDecoration(
        borderRadius: BorderRadius.round(20),
        colour: Colours.white,
        picture: DecorationImage(
          picture: AssetImage("belongings/piliers.png"),
        ),
        boxShadow: [
          BoxShadow(
            blurRadius: 3,
            offset: Offset(5, 5),
            color: color.AppColor.gradientSecond.withOpacity(0.1),
          ),
          BoxShadow(
            blurRadius: 3,
            offset: Offset(-5, -5),
            color: color.AppColor.gradientSecond.withOpacity(0.1),
          ),
        ],
      ),
      little one: Middle(
          little one: Align(
        alignment: Alignment.topRight,
        little one: Container(
          top: 28.83,
          width: 50,
          ornament: BoxDecoration(
            colour: Colours.orangeAccent,
            borderRadius: BorderRadius.solely(
                topRight: Radius.round(20), bottomLeft: Radius.round(20)),
            //border: Border.all(colour: colour.AppColor.setsColor),
          ),
          little one: Middle(
            little one: Textual content(
              "$score1",
              fashion:
                  TextStyle(colour: Colours.black, fontWeight: FontWeight.daring),
            ),
          ),
        ),
      )),
    );
  }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments