Search code examples
fluttertextfieldprovider

How can I pass data from a TextFieldInput to another Page with Provider


I want to pass the Data (String) from a TextField to a second Page with Provider after I clicked the Button.

Here is my code


Solution

  • Update your name_provider class

    class NameProvider extends ChangeNotifier {
      String _name = "";
    
      String get getName => _name;
    
      saveName(String name) {
        _name = name;
        notifyListeners();
      }
    }
    

    The name variable was made private to avoid getting wrong result while calling the function.

    Now this edit was made to main.dart file

    class _MyHomePageState extends State<MyHomePage> {
      String name = "";
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Padding(
            padding: const EdgeInsets.all(20),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  const Text(
                    'Please enter your name',
                  ),
                  TextField(
                    onSubmitted: (value) {
                      setState(() {
    Provider.of<NameProvider>(context).saveName(value);
                      });
                    },
                    onChanged: (value) {
                      setState(() {
                        name = value;
                      });
                    },
                  ),
                  Text("Name: $name"),
                  TextButton(
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const SecondPage(),
                        ),
                      );
                    },
                    child: const Text("To Second Page"),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    Now getting name in SecondPage:

    class SecondPage extends StatelessWidget {
      const SecondPage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("Second Page"),
          ),
          body: Padding(
            padding: const EdgeInsets.all(20),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  Text(
                    "Name:",
                    style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                  ),
                  Text(
                    
                    Provider.of<NameProvider>(context).getName,
                    style: TextStyle(fontSize: 32),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    

    If there is some sort of expected string but got... error, replace getName with '${Provider.of<NameProvider>(context).getName}'

    Let us know if this solution works