Flutter - Navigation to Previous Screen using GetX Library Function
Last Updated :
23 Jul, 2025
When we are using any app then we do navigation to navigate between screens. Sometimes, we want to return to the previous screen, so we normally use Navigator.pop(context). This is using context, and sometimes we find shortcuts to do the task easily. For that, we haveGet.back()in flutter. We can send the result to the previous route and do the operations.
Syntax:
Get.back()
You can also send a result back to the previous screen:
Get.back(result: "Returned Data");
Step By Step Implementation
Step 1: Create a new Flutter Application
Create a new Flutter application using the command Prompt. To create a new app, write the below command and run it.
flutter create app_name
To know more about it refer this article: Creating a Simple Application in Flutter
Step 2: Adding the Dependency
To add the dependency to the pubspec.yaml file, add get as a dependency in the dependencies part of the pubspec.yaml file, as shown below:
Dart
dependencies:
flutter:
sdk: flutter
get: ^4.7.2
Now run the below command in the terminal.
flutter pub get
Or
Run the below command in the terminal.
flutter pub add get
Step 3: Import dependencies
To use libraries, import all of them in the respective .dart file,
import 'package:get/get.dart';
Step 4: Implementing Get.back()
Now, write code to implement Get.back(). For that, we should have two screens. and first, we navigate to a new screen and get back to the previous screen with some data.
Simple Navigation Back
Example:
Dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// Entry point of the Flutter application
void main() {
runApp(MyApp());
}
// Root widget of the application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue), // Application theme
home: Page1(), // Initial screen of the app
debugShowCheckedModeBanner: false,
);
}
}
// First page of the application
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeekforGeeks GFG"),
centerTitle: true, // Centers the title
backgroundColor: Colors.green,
foregroundColor: Colors.white, // Text color of the AppBar
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Centers the content vertically
children: [
Text("Page 1", textScaleFactor: 2), // Displays "Page 1" text
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white
),
child: Text("Navigate to next screen"),
onPressed: () {
Get.to(Page2()); // Navigates to Page2
},
),
),
],
),
),
);
}
}
// Second page of the application
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeekforGeeks GFG"),
backgroundColor: Colors.green,
foregroundColor: Colors.white, // Text color of the AppBar
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Centers the content vertically
children: [
Text("Page 2", textScaleFactor: 2), // Displays "Page 2" text
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white
),
child: Text("Navigate to previous screen"),
onPressed: () => Get.back(), // Navigates back to the previous screen
),
),
],
),
),
);
}
}
Output: In this, we are not sending any status get after returned back. We are simply getting back using Get.back().
Return Data Using Get.back(result: ...)
Example:
Dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// Entry point of the Flutter application
void main() {
runApp(MyApp());
}
// Root widget of the application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue, // Theme color
),
home: Page1(), // Initial screen of the app
debugShowCheckedModeBanner: false,
);
}
}
// First page of the application
class Page1 extends StatefulWidget {
@override
_Page1State createState() => _Page1State();
}
class _Page1State extends State<Page1> {
String? x; // Variable to store data returned from Page2
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeekforGeeks GFG"),
centerTitle: true, // Centers the title
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center, // Centers content vertically
children: [
Text(
"Page 1",
textScaleFactor: 2, // Scales the text size
),
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
child: Text("Navigate to next screen"),
onPressed: () async {
// Navigates to Page2 and waits for the result
x = await Get.to(Page2());
setState(() {
// Updates the UI with the returned data
});
},
),
),
Text(
x ?? x.toString(), // Displays the returned data or null
textScaleFactor: 2, // Scales the text size
),
],
),
),
);
}
}
// Second page of the application
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GeekforGeeks GFG"),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
body: Center(
child: Column(
mainAxisAlignment:MainAxisAlignment.center, // Centers content vertically
children: [
Text(
"Page 2",
textScaleFactor: 2, // Scales the text size
),
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
),
child: Text("Navigate to previous screen"),
onPressed:
() => Get.back(
result:
"Data after returning to first page", // Data sent back to Page1
),
),
),
],
),
),
);
}
}
Output:
In this, we are sending results back to the homepage using Get.back( result: "Data after returning to first page"). And receiving result usingx= await Get.to(Page2()); in First page.
Explore
Basics
Key Widgets
UI Components
Design & Animations
Forms & Gestures
Navigation & Routing
Hardware Interaction
Sample Flutter Apps
Advance Concepts