Home > Mobile >  flutter not updating the app on state change
flutter not updating the app on state change

Time:03-18

import 'package:flutter/material.dart';
import 'data.dart';

class Game extends StatefulWidget
{
  Game({Key? key}) : super(key: key);

  static Data data = Data();
  static int counter = 0;

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

class _GameState extends State<Game>
{
  int counter2 = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Meh',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(Game.counter.toString(), ),
              Text(counter2.toString(), ),
              OutlinedButton(
                onPressed: () {
                  counter2  ;
                  Game.counter  ;
                  print(counter2);
                  print(Game.counter);
                  Game.data.save();
                  setState(() {});
                },
                child: Text("SAVE"),
              ),
              OutlinedButton(
                onPressed: () {
                  Game.data.load();
                  setState(() {});
                },
                child: Text("LOAD"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

I have to be doing something super stupid but I can't figure out what. You would expect when hitting the save button that the counter variable gets increased and displayed on screen but it's not displaying. Flutter is just ignoring the changes.

Maybe it's something wrong with my emulator? When I switch back to the editor and back to the emulator the value is showing correctly like flutter redrew itself from switching windows but not by clicking the button.

EDIT: This looks like the same issue I'm having, https://github.com/flutter/flutter/issues/17155, any ideas on how to fix it?

CodePudding user response:

I tried your code, and it works for me.

It's definitely an IDE problem.

Try to update Flutter and also your IDE.

To be able to help you, please execute this command "flutter doctor" , and send us the result.

Cordially

CodePudding user response:

You can run in https://www.dartpad.dev/, and see youself that the code is working as you expect.

  • Related