0

How to call setState() from Category.dart file?

In main.dart:

body: Column(
      children: <Widget>[
        slideShow(),
        category() // from Category.dart
      ],
    )

In Category.dart:

category() {
  return _girdView();
}

_girdView() {
  return Center();
}
mducc
  • 743
  • 1
  • 9
  • 32

3 Answers3

1

You can refer to this answer:

Flutter setState to another class?

Honestly this more of a hack than a solution, it's a limitations of setState if your project is relatively small then it's fine but otherwise you should be using ScopedModels or BLocs or Streams instead

Nadeem Siddique
  • 1,988
  • 4
  • 14
  • 20
0

setState() is called only for the same class, when you call setState() your widget gets rebuild. If you want to call setState of the parent then you can use StateUp giving a call back to parent class method from the child class. If you want to call child class setState() then you can use GlobalKey and then pass to child class and then call child method, under this method you can define setState(); for more complicated state management you can use ScopeModel, InheritWidget, Redux, Bloc, or Stream. Flutter State Management

satish
  • 1,304
  • 3
  • 13
  • 33
0

I advise you use Provider as it was just recently recommended by the Google team as the best method. Provider will make sure you keep your code clean no matter how big your project gets. It's like ScopedModel but on steroids.

Flutter I/O presentation by the Flutter team: https://www.youtube.com/watch?v=d_m5csmrf7I

Package: https://pub.dev/packages/provider#-readme-tab-

Leoog
  • 244
  • 2
  • 8