when i press the Button in the AppBar for changeName its works, but why does all the values in the Class Hui extends Model Class reset after i use a Button with a setState?

e.g. ...setState(() { tmp = 1; });

how can i avoid it?

enter image description here

class Hui extends Model {
  String name = "Hi";

  void changeName() {
    name = "Hello World";
    notifyListeners();
  }
}

class _MyAppState extends State<MyApp> {
      int tmp = 0;
      @override
      Widget build(BuildContext context) {
        return new ScopedModel<Hui>(
          model: Hui(),
          child: Scaffold(
            appBar: AppBar(
              title: Text('State Test'),
              actions: <Widget>[
                ScopedModelDescendant<Hui>(
                  builder: (context, child, model) => IconButton(
                        icon: Icon(Icons.perm_phone_msg),
                        onPressed: () {
                          model.changeName();
                        },
                      ),
                ),
              ],
            ),
            body: Column(
              children: <Widget>[
                ScopedModelDescendant<Hui>(
                    builder: (context, child, model) => Text(model.name)),
                IconButton(
                  icon: Icon(Icons.perm_phone_msg),
                  onPressed: () {
                    setState(() {
                      tmp = 1;
                    });
                  },
                ),
              ],
            ),
          ),
        );
      }
    }


Solution 1: Kirill Shashov

You can save your model in widget state, then it will not be recreated every time in the build method.

Something like this:

class _MyAppState extends State<MyApp> {
    Hui _hui = Hui();

    ....
    @override
    Widget build(BuildContext context) {
      return new ScopedModel<Hui>(
        model: _hui,
        ...