Ashtav
Ashtav

Reputation: 2966

How to catch value when back to previous screen?

this is my second class

class SecondClass extends StatefulWidget {
   _SecondClassState createState() => _SecondClassState();
}

class _SecondClassState extends State<SecondClass> {
  @override
  Widget build(BuildContext context) {
    Return Container(
      RaisedButton(
       onPressed: Navigator.of(context).pop('lorem ipsum),
       child: Text('Back and get data')
      )
    );
  }
}

this is my first class

class FirstClass extends StatefulWidget {
  _FirstClassState createState() => _FirstClassState();
}

class _FirstClassState extends State<FirstClass> {
   @override
   Widget build(BuildContext context) {
     Return Container(
       // show data here
     );
  }
}

How to get string of lorem ipsum and display it in first class, where I should put the code to get that string?

Upvotes: 0

Views: 424

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 267554

enter image description here

You can see in the screenshot, whatever item is clicked in 2nd screen, it is sent back to page 1 and Button shows the same item.


Here is the full code for a basic implementation.

void main() {
  runApp(MaterialApp(home: Page1()));
}

class Page1 extends StatefulWidget {
  @override
  _Page1State createState() => _Page1State();
}

class _Page1State extends State<Page1> {
  String _response = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Page 1")),
      body: Center(
        child: RaisedButton(
          child: Text("Go to Page 2"),
          onPressed: () {
            Navigator.push(context, MaterialPageRoute(builder: (context) => Page2())).then((value) {
              setState(() {
                _response = value; // you receive here
              });
            });
          },
        ),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Page 2")),
      body: ListView.builder(
        itemCount: 20,
        itemBuilder: (c, i) {
          return ListTile(
            title: Text("Item ${i}"),
            onTap: () {
              Navigator.pop(context, "Item ${i}"); // what you pass here
            },
          );
        },
      ),
    );
  }
}

Upvotes: 1

CopsOnRoad
CopsOnRoad

Reputation: 267554

Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondClass())).then((value) {
  // value is lorem ipsum
});

You should use this in your FirstClass when you are navigating to the SecondClass.


Full solution:

class _FirstClassState extends State<FirstClass> {
  String _string = "";

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      child: Text("Go"),
      onPressed: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondClass())).then(
          (value) {
            setState(() {
              _string = value; // lorem ipsum
            });
          },
        );
      },
    );
  }
}

Upvotes: 0

Related Questions