Home > Mobile >  How to get TextField content from screen to screen?
How to get TextField content from screen to screen?

Time:11-12

still new to flutter and I was trying to take multiple values from TextFields in a form to display them in a new screen inside multiple Text elements. Can someone explain how to do it ?

CodePudding user response:

There are three ways to do it

First method: You can define a class and assign values to it like this:


class Global(){

String text;

} 

and then you can import it and assign values or use it like this:


// assign data
Global().text = TextField_controller; // I assume you have already implemented a TextField 

// use it 

Text(Global().text)

This method is good for passing data between multiple pages but it's not recommended because you can't update the screen when the value changes, it's only good when you need to pass a static variable between multiple pages, for example a user name


Second method: passing data to next page directly

Make the SecondScreen constructor take a parameter for the type of data that you want to send to it. In this particular example, the data is defined to be a String value and is set here with this.text.

class SecondScreen extends StatelessWidget {
  final String text;
  SecondScreen({Key key, @required this.text}) : super(key: key);

  ...

Then use the Navigator in the FirstScreen widget to push a route to the SecondScreen widget. You put the data that you want to send as a parameter in its constructor.

Navigator.push(
    context,
    MaterialPageRoute(
      builder: (context) => SecondScreen(text: 'Hello',),
    )); 

this method is great for passing data from a parent page to a child page however it can quickly become a nightmare if you want to pass the data to several children down the widget tree or move data back to the parent widget, in that case you can use method 1 or


Third method: using Provider, which is the recommended way, it is similar to the first method but with provider you can ``notify``` all of the listeners to the provider class, meaning you can update the widget whenever the the variable updates, I strongly recommend reading the documentation or watching some YouTube videos, but in short you can use it like this:

after installing the provider package you define your class:

Global extends ChangeNotifierProvider(){

String text;

}

and then add it to the root of your app in main.dart:

Widget build(BuildContext context) {
  return MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => Global()),
      ),
    ],
    child: MyApp(),
  );
}

and then you define your provider wherever you need to use it


Provider.of<Global>(context, listen: false); // Note that if you want to listen to changes you have to set listen to true

// then  you can access your variable like in method 1 

insatnce.text = TextField_controller;

// and then you can use it anywhere 

Text(instance.text);

again if you find this confusing read the documentation or watch some videos

  • Related