Abrar Lasebai
Abrar Lasebai

Reputation: 93

how to navigate data between two screens in flutter?

I have homeScreen, which every another screen returns to it. and one of the screens have parameters from TextField, which I need in the homeScreen, how I can navigate these parameters to the home? NOTE: when I use constructor, the other screens show an error in the line that navigate to Home because there is no parameters.

Upvotes: 0

Views: 466

Answers (2)

Kapil Sahu
Kapil Sahu

Reputation: 599

You can pass data using parameters

    class XYZ extends StatelessWidget {
      final TextEditingController textController = TextEditingController();
    
      void navigationFunction() {
//send the data from XYZ to HomePage
        Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => HomePage(textValue: textController.text)),
        );
      }
    }
        
    class HomePage extends StatelessWidget {
      const HomePage({this.textValue});
      final String textValue; //use this textValue in HomePage
    
      @override
      Widget build(BuildContext context) {
        return Scaffold();
      }
    }

Upvotes: 0

BLKKKBVSIK
BLKKKBVSIK

Reputation: 3548

You can either use an optional parameter in your constructor:

Homepage({String textfield});

and use it on your Homepage (don't forget that this value is nullable)

Or you need to use some kind of state management with ValueNotifiers

Upvotes: 1

Related Questions