Flutter

Update State In Flutter


In this article, you will learn How to Update the State in Flutter.

Creating State

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}

Above you have a empty Stateful widget with empty container.

Creating Variable

String text = 'code-source.tempurl.host';

Here you have created a variable which you will change when button is clicked.

Creating Button

OutlineButton(
            child: Text('Change State'),
            onPressed: () {
              setState(() {
                text = 'code-source.tempurl.host is great';
              });
            },
          )

Above you have an OutlineButton() with onPressed. To change the state you use the setState method which takes a callback and then you can easily change the state.

Result


Share on social media

//