If you have started your journey in flutter apps , You might have wondered how to pass data between pages and routes. This article will explain you that.
Block diagram
Suppose you want to pass data like age,name etc to second page
Step-1 ( pass the values while routing to another page)
Navigator.push( context, new MaterialPageRoute(
builder: (BuildContext context) => new ScreenTwo("Screen Two")));
}
// you can see above I am trying pass a string " Screen two " here to Screen two page.
Whose code is like this.
Step -2 (put provision to catch those values as variables in the other page)
import 'package:flutter/material.dart'; class ScreenTwo extends StatefulWidget {
ScreenTwo(this.title) : super(); // arguement declared through "this.title." final String title; // variable declared @override _ScreenTwoState createState() => _ScreenTwoState(); } lass _ScreenTwoState extends State<ScreenTwo> {
void goBack(BuildContext context) {
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title), // now access the value any where just used " widget.<variable name>"
),
...............(Code continues)
Procedure.
Output
Reference
code
https://github.com/coeaimrm/passingdatabetweenscreens/tree/master
Comments