How to Import Data From One Page to Another in Flutter?
Last Updated :
13 Jun, 2022
Flutter is Google’s Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), Web apps from a single codebase. When building applications with Flutter everything towards Widgets – the blocks with which the flutter apps are built. They are structural elements that ship with a bunch of material design-specific functionalities and new widgets can be composed out of existing ones too. In this article, we are going to find the solution for the problem statement "How to import/send data from one page to another in flutter". Before going into the topic, there are some pre-requisites.
Pre-requisites:
Let us dive into the article by creating a simple app with two pages. We have used VS Code IDE for the development.
Terminologies we are going to use in this app:
Step by Step Implementation
Step 1:
Creating a new Flutter app:
- Use the command ctrl+shift+p to open Command Palette.
- Type Flutter: New Project in the palette.

Note: If you are using Android Studio IDE, then use the path File-> New-> New Flutter Project.
Step 2:
Adding Textfield to get data from users:
- Create Textfields to get input from the user.
- Create TextEditingController classes for the appropriate TextField.
Syntax:
TextEditingController object_name = TextEditingController();
Mention the object name of the Controller inside the TextField class using the controller attribute. Refer to below code,
child: TextField(
// controller attribute.
controller: controller_object_name,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Enter your Phone Number"
),
),

Step 3:
Sending data and Navigating to the next page:
- To navigate to the next page we use Navigator with the main.dart's context.
- To send the data as arguments while navigating to the next page, we use MaterialPageRoute class with the builder for the nextpage class's constructor.
- We should create a Button to perform the above functions in the onPressed: () function by clicking it. For this, we are going to use ElevatedButton class for the creation of buttons.
ElevatedButton(
onPressed: () {
// Navigator for next page.
Navigator.of(context).push(
MaterialPageRoute(
// Builder for the nextpage
// class's constructor.
builder: (context) => nextpage(
name: _name.text,
email: _email.text,
phoneno: _phoneno.text,
)),
);
},
child: Text("SEND")
),
main.dart file:
Dart
import 'package:flutter/material.dart';
import 'package:geeksforgeeks/nextpage.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const MyHomePage(title: 'GeeksForGeeks'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// To listen to the changes in the textfield.
TextEditingController _name = TextEditingController();
TextEditingController _email = TextEditingController();
TextEditingController _phoneno = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(25),
child: TextField(
// To set the appropriate
// controller to the text field.
controller: _name,
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: "Enter your Name"),
),
),
Padding(
padding: const EdgeInsets.all(25),
child: TextField(
controller: _email,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Enter your Email"),
),
),
Padding(
padding: const EdgeInsets.all(25),
child: TextField(
controller: _phoneno,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Enter your Phone Number"),
),
),
// Button to go to the nextpage.
ElevatedButton(
onPressed: () {
// Navigator to the next page.
Navigator.of(context).push(
MaterialPageRoute(
// Builder for the nextpage
// class's constructor.
builder: (context) => nextpage(
// Date as arguments to
// send to next page.
name: _name.text,
email: _email.text,
phoneno: _phoneno.text,
)),
);
},
child: Text("SEND"))
],
),
),
);
}
}
Step 4:
Create a new dart File:
- Using the path File-> New File creates a new dart file with name nextpage.dart inside the lib folder.
- Import the package 'package:flutter/material.dart'. This package is used to implement the material design of the app.
- Create class nextpage() which extends StatelessWidget.
- Create a constructor for the class to receive the arguments from the previous page's class and save the arguments as variables.
String name, email, phoneno;
nextpage({required this.name, required this.email, required this.phoneno});
Create Textfields and its appropriate TextEditingController and set the data from the previous page in the respective text fields using the text attribute. Refer to the below code,
TextEditingController object_name = TextEditingController(text: data);

nextpage.dart file:
Dart
import 'package:flutter/material.dart';
class nextpage extends StatelessWidget {
String name, email, phoneno;
// Constructor to get the data from the previous page.
nextpage({required this.name, required this.email, required this.phoneno});
@override
Widget build(BuildContext context) {
// To listen to the changes in the textfield.
TextEditingController _name = TextEditingController(text: name);
TextEditingController _email = TextEditingController(text: email);
TextEditingController _phoneno = TextEditingController(text: phoneno);
return Scaffold(
appBar: AppBar(
title: Text('Next Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(25),
child: TextField(
// To set the appropriate controller
// to the text field.
controller: _name,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Name",
),
),
),
Padding(
padding: const EdgeInsets.all(25),
child: TextField(
controller: _email,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Email",
),
),
),
Padding(
padding: const EdgeInsets.all(25),
child: TextField(
controller: _phoneno,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: "Number",
),
),
),
],
),
),
);
}
}
Output:
Similar Reads
How to Import Local Package in Flutter?
Flutter is a framework that is used for developing cross-platform applications for Android, iOS, Linux, macOS, Windows, and other web applications using a single source code and it was developed by Google. Flutter is a free and open-source software development kit for developing mobile applications.
4 min read
Flutter - Navigate From One Screen to Another
Flutter apps may have multiple screens or pages. Pages are groups of functionalities. The user navigates between different pages to use different functionalities. Concepts like pages are called routes in Flutter. We can use Navigator.push() to navigate to a new route and Navigator.pop() to navigate
3 min read
Flutter - Pass a Function as an Argument From One Screen to Another
In Flutter, you can pass a function as an argument from one screen (widget) to another screen (widget) by defining a callback function and passing it as an argument when navigating or pushing to the new screen. Here's a step-by-step guide on how to do this: Required Tools To build this app, you need
4 min read
How to Import a Flutter Project from GitHub?
Importing a project from GitHub is a very common practice of developers. When we want to get any project from an online source like GitHub then we can easily import or download it to our system. Sometimes, developers need to get the small module of application and if that module is available on GitH
2 min read
How to Import Existing Flutter Project in Android Studio?
In this article, we are going to see the process of importing an existing flutter Project into the Android Studio. This is going to be quite easy, we just have to take care of some steps involved in it. Before going on the main topic let's have brief info about Flutter and Android Studio as well. Fl
3 min read
Flutter - Extract Data From an Image to Text
Google ML kit provides many features, one of them is Image to Text Extraction, through which we can simply extract text from an image. To extract text from the image first we need to take an image as input either from the gallery or camera for which we will use 'image_picker: ^1.0.4' (dependency fro
3 min read
Flutter - Pass Data One Screen To Another Screen
In this article we gonna know how you can pass the data from one screen to another screen in Flutter. In this example we took two screens one and screen two, In the first screen, you enter the data, and that data you can see on your second screen. This is the main object of this application. If we t
4 min read
How to use Functions of Another File in Flutter?
Flutter is an open-source framework by Google for building beautiful, natively compiled, multi-platform applications from a single codebase. A Single codebase means that only we have to write a single code for Android and IOS and others like Desktop  Application, Web Application. So, Today we are go
2 min read
How to Save the File in Phone Storage in Flutter?
Like in Instagram, we can save stories, and reels on our devices. We also want to save some information or some file on our device in android we will keep the file from the flutter app on android devices. Step By Step Implementation Step 1: Create a New Project in Android Studio To set up Flutter De
3 min read
How to Implement Fancy Bottom NavigationBar in Flutter?
Fancy Bottom NavigationBar is a MaterialApp widget that displays a bottom fancy navbar, Where items go between 2 and 4. Bottom NavigationBar is the list of icons or text in the horizontal form called a tab and Each tab contains their own page. Step By Step ImplementationStep 1: Create a New Project
3 min read