Flutter - Push and Get value between routes

前端 未结 5 615
情话喂你
情话喂你 2021-02-04 02:19

How do I send the green string from the HomePage page to the ContaPage page?

I think it\'s so Navigator.of(context).pushNamed(\'/conta/green\'); but I do no

5条回答
  •  执念已碎
    2021-02-04 03:00

    Passing data from 1st Page to 2nd

    In 1st page

    // sending "Foo" from 1st page
    Navigator.push(context, MaterialPageRoute(builder: (_) => Page2("Foo")));
    

    In 2nd page.

    class Page2 extends StatelessWidget {
      final String string;
      Page2(this.string); // receiving "Foo" in 2nd
    }
    

    Passing data back from 2nd Page back to 1st

    In 2nd page

    // sending "Bar" from 2nd
    Navigator.pop(context, "Bar");
    

    In 1st page, it is the same which was used earlier but with little modification.

    // receiving "Bar" in 1st
    String received = await Navigator.push(context, MaterialPageRoute(builder: (_) => Page2("Foo")));
    

提交回复
热议问题