0

I want to fetch the variable of other Stateful Widget

For example, I have three widget such as SmartTag, MySlider, MyHomePage

MyHomePage has SmartTag and MySlider

(this code is not correct just the bulky image)

class _SmartTagState extends State<_SmartTagState>{ var tagState; class _MySliderState extends State<_MySliderState>{ var sliderState; class MyHomePage extends StatefulWidget {} class _MyHomePageState extends State<MyHomePage> { return Row( children:[ MySlider(), SmartTag() floatingActionButton: FloatingActionButton( onPressed: (){ print(MySlider.sliderState)// I want to get the variable fron another widget. print(SmartTag.tagState)// I want to get the variable fron another widget. }, tooltip: 'Increment', child: const Icon(Icons.add), ), ] ) } 

What I want to do is, getting the other widget varliables when button is pushed.

So,I have some quesitons.

  1. Is there any way to get the other widgets(SmartTag,MySlider) variable from _MyHomePageState?
  2. _MyHomePageState should have the all variables from the beginning? (However when code is long and comples, it's not useful)

Any help appreciated thank you .

1
  • 1
    You need to use voidcallback functions to pass data between widgets. You can find many videos on YouTube if you search "voidcallback flutter". Another way is to use provider to get the data across any widget in your application. Commented May 20, 2022 at 16:56

2 Answers 2

2

you can pass a callback function to your child widgets and update corresponding variable for that widget inside MyHomePage:

class MyHomePage extends StatefulWidget { const MyHomePage({ Key? key }) : super(key: key); @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { var tagValue = ... var sliderValue = ... @override Widget build(BuildContext context) { return Row( children:[ MySlider( onChange: (value){ setState(() { sliderValue = value; }); } ), SmartTag( onChange: (value){ setState(() { tagValue = value; }); } ) floatingActionButton: FloatingActionButton( onPressed: (){ print(MySlider.sliderState)// I want to get the variable fron another widget. print(SmartTag.tagState)// I want to get the variable fron another widget. }, tooltip: 'Increment', child: const Icon(Icons.add), ), ] ); } } 

Your child widgets would be something like this:

class SmartTag extends StatefulWidget { const SmartTag({ Key? key ,required this.onChange}) : super(key: key); final Function(dynamic value) onChange; @override State<SmartTag> createState() => _SmartTagState(); } class _SmartTagState extends State<SmartTag> { @override Widget build(BuildContext context) { return Container( ); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much ,I'm trying now, but when onChange is called in child widget?
I call widget.onChange(value) in _SmartTagState and it works.
0

Define the variables you want to pull as static and use them like this

class _SmartTagState extends State<_SmartTagState>{ static tagState; class _MySliderState extends State<_MySliderState>{ static sliderState; class MyHomePage extends StatefulWidget {} class _MyHomePageState extends State<MyHomePage> { return Row( children:[ MySlider(), SmartTag() floatingActionButton: FloatingActionButton( onPressed: (){ print(MySlider.sliderState)// I want to get the variable fron another widget. print(SmartTag.tagState)// I want to get the variable fron another widget. }, tooltip: 'Increment', child: const Icon(Icons.add), ), ] ) } 

1 Comment

Thank you for your help.In this case I use callback method though, Static should be the idea to be considered. thankyou .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.