I am new in flutter and I have a problem which is where to put the setStat() method. here is a simple code that I made to train, it is a stop watch. in debugging, every thing goes well, but the screen doesn't update, it only shows the initial state which is "00:00:00".

here is the code:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

var time = new Stopwatch();

class _MyAppState extends State<MyApp> {
  int counter = 0;
  bool stat = false;
  String timeFormate = "00:00:00";

  setStat() {
    if (stat) {
      timeFormate = time.elapsed.toString();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Timer app pro',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("Training fucken app"),
        ),
        body: Container(
          child: Column(children: [
            Text(timeFormate),
            RaisedButton(
              child: Text("Start"),
              onPressed: () {
                time.start();
                stat = true;
                print(time.isRunning);
                print(time.elapsed.toString());
              },
            ),
            RaisedButton(
              child: Text("Stop"),
              onPressed: () {
                time.stop();
                print(time.isRunning);
                print(time.elapsed.toString());
              },
            ),
            RaisedButton(
              child: Text("Reset"),
              onPressed: () => time.reset(),
            )
          ]),
        ),
      ),
    );
  }
}


Solution 1: Philip Dolenko

1) "setStat" is wrong, try using "setState". 2) you are missing extra "( )" without them ou declare a new method (aka function) what you need is pass your function inside setState as an argument So lets recap

1) call function "setState()";

2) pass your function as an argument

void foo (){
  if (stat) {
     timeFormate = time.elapsed.toString();
  }
}

setState(foo);

3) use a "short" syntax replace foo in 'setState(foo);' to be a body of 'foo' function

setState(() {
  if (stat) {
   timeFormate = time.elapsed.toString();
  }
});

Also you can move if statement outside of setState

  if (stat) {
   setState(() {
     timeFormate = time.elapsed.toString();
   });
  }