Screen not updating in a statefulWidget in Flutter

Issue

I have a statefulWidget in Flutter like this:

class GameScreen extends StatefulWidget {
  @override
  GameScreenState createState() => GameScreenState();
}

class GameScreenState extends State<GameScreen> {
  List<String> selectedWord = ['h', 'e', 'l', 'l', 'o'];

  Widget _letterInput() {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          for (var letter in selectedWord) LetterInput(letter: letter)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          _letterInput(),
        ],
      )),
    );
  }
}

class LetterInput extends StatelessWidget {
  LetterInput({this.letter});

  final String letter;

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
        decoration: BoxDecoration(
            border: BorderDirectional(
                bottom: BorderSide(width: 6.0, color: Colors.green))),
        child: Text(letter,
            textAlign: TextAlign.center,
            style:
                GoogleFonts.acme(fontSize: 28.0, fontWeight: FontWeight.w400)));
  }
}

The problem is that when I first launch the app with this widget, I can see hello on the screen, but if I go on and change hello to hellos in selectedWord, that does not update the screen and it still shows me hello even though the hot reload is turned on. I have to go and restart the app so it shows me hellos. How could I fix this?

Solution

In my experience, hot reload keeps states. Try hot restart instead?

Referring to your comment, if you want to keep using hot reload, I suggest you pull out the variable to your widget itself (if that is an option for you), like this:

class GameScreen extends StatefulWidget {
  final List<String> selectedWord = ['h', 'e', 'l', 'l', 'o'];

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

class GameScreenState extends State<GameScreen> {
  Widget _letterInput() {
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          for (var letter in widget.selectedWord) LetterInput(letter: letter)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              _letterInput(),
            ],
          )),
    );
  }
}

Answered By – kazume

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *