Flutter – Navigation to Next Screen using GetX Library Function
When we want to navigate between the screens in flutter then we use Navigator in Flutter. It uses context and builders for navigation. Sometimes, we pass the data to the next screen and fetch data on the next screen. When we use Navigator in flutter then its syntax is very large and it’s not a good practice every time when we navigate to other screens. To simply our work, we have GetX library that provides so many functionalities. With GetX, we can navigate to the next screen using Get.to() and can return to the previous screen using Get.back(). We can also pass or retrieve data between screens.
Follow the steps to implement Get.to() :
- Create a new Flutter application:
flutter create APPLICATION_NAME
- Add get package in pubspec.yaml file under dependencies:
- Import the package in main.dart:
- Write some code to create two screens. Remember to use GetMaterialApp instead of MaterialApp when building an app.
Output: We will navigate to the next screen using Get.to() without passing data.
Output: We are passing data using Get.to(SecondScreen(), arguments: “Data received from first screen”).And receiving in next screen using Text(Get.arguments.toString()).