How to reach a variable of a class from another class in Flutter

Viewed 17096

I have a stateful widget which includes buttons. I'm keeping number of user taps in an int variable called "counter".

I have another widget which is my home screen. I have a Text on that screen. I need to show the number of taps on this Text dynamically. So when user taps the button on the first class, my Text on the second class must be updated.

What is the best way to pass the counter variable from first widget to second widget?

Added some code

Both of my classes are stateful widgets. So, I declared static int counter like these;

class AppListView extends StatefulWidget {
  static int counter;

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

Now I can reach counter from other class but can't reach from its own class. I mean I need to increase counter in that part but It says undefined.

class _AppListViewState extends State<AppListView> {
@override
  Widget build(BuildContext context) {
    return FlatButton(
           onPressed: (){
              counter++ (can't reach counter here)
      }
4 Answers

You might want to make your desired variable static.

class Something {
   static int counter; 
}

Then you can use the variable in the other widget like so:

class StatefulWidget{
   FlatButton(
    onPressed: (){
      Something.counter++; // This variable is your counter you mentioned earlier
   }
);
}

Approach 1:

Create a ValueChanged from Parent Widget add pass to FirstWidget, when change couter -> call ValueChanged to send update to Parent Widget, Parent will update state for second Widget.

Approach 2:

Create a Stream from Parent Widget add pass to FirstWidget and Sencond Widget (using StreamBuilder to render widget). when change couter -> change value of Stream to update to Second Widget.

Update Demo:

Link DartPad

Simple, in your code just write "widget.counter" in child. In statefull widget to access parent variable, you have to use "widget.instace". In this case, "widget.counter" will let u access counter variable of the parent.

  class AppListView extends StatefulWidget {
    int counter;

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

  class _AppListViewState extends State<AppListView> {
  @override
    Widget build(BuildContext context) {
      return FlatButton(
            onPressed: (){
                widget.counter++
        }

Note: u don't need counter variable to be static as u can call it with widget.counter in child class

You should use the provider package to associate the object with Provider, not any widget. Wrap the counter inside a class and pass it to MultiProvider:

void main() {
  runApp(
    MultiProvider(
      providers: [
        Provider(create: (context) => Something()),
      ],
      child: const MyApp(),
    ),
  );
}

And then you can access it from your build methods in each widget by:

var something = Provider.of<Something>(context, listen: false);
something.value++;
Related