Home > OS >  Passing data to a deep ancestor widget in flutter
Passing data to a deep ancestor widget in flutter

Time:11-28

I have eight screens. The first 7 screen has data to be sent to the last screen how do I go arouund this??

I tried passinng it through from one parent to another but that is too much work

CodePudding user response:

why don't you use a state management like provider??? if you don't use a state management it'll be hard to use datas from one screen to another step by step. you can create a class like this:

class ExpampleClass extends ChangeNotifier {

String? _yourData;

void setYourData(String? newData){
_yourData = newData;
 notifyListeners();
}

String? get yourData => _yourData;

}

as you see when _yourData is changed, it tells you and you can use this data where ever you want by providing ExpampleClass,even you can set a data in your first screen and use that data in the last screen without passing data step page by step.

Provider.of<ExpampleClass>(context, listen: false).yourData;

and even you can use that data in your widgets like this by using Consumer everywhere you want:

Consumer<ExpampleClass>(
        builder: (context, exampleClassProvider ,snapshot) {
          return Text(exampleClassProvider!.yourData);
        }
      )

***be careful to use MultiProvider in your first root class of your project to define your providers.

here is provider package document. read it carefully.

CodePudding user response:

you need an global state solution, like provider, riverpod, getx etc … there’s good book on this in amazon "Managing State in Flutter Pragmatically”

  • Related