SoloBits Share | Learn | Contribute

Routing Using Get

Routing in Flutter can be easy for small apps, but when you have quite a few pages you know that you need to manage them properly. Flutter gives you quite a few ways to do that, you might be doing it by using named routes or a package like auto_route. For this article, we’ll be using GET.

GET, Router

GET is not just a simple Routing solution but a Micro-Framework for Flutter. It has quite a lot that we can do with it like Dependency Management, State Management, Utils/Helpers and more.

Routing

We’ll start by creating a new Flutter project.

Importing Package

In pubspec.yaml import GET package. pubspec will then look something like this:

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.3
  get: ^3.2.2

dev_dependencies:
  flutter_test:
    sdk: flutter

    

Base Setup

In your main.dart rename MaterialApp to GetMaterialApp.

import 'package:get/route_manager.dart';

import 'package:demo/routes.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      initialRoute: routeOne,
      getPages: generateRoutes
    );
  }
}

Create a routes.dart file

import 'package:get/route_manager.dart';

import 'package:demo/pages/one_page.dart';
import 'package:demo/pages/two_page.dart';

const String routeOne = '/';
const String routeTwo = '/two';
mixin AppRoutes {
  final List<GetPage> generateRoutes = [
    GetPage(
        name: routeOne,
        page: () => const PageOne()),
        GetPage(
        name: routeTwo,
        page: () => const PageTwo()),
   
  ];
}

Now simply call Get.toNamed to navigate to a page or Get.offNamed to push to new page and replace current page.

//Navigator.pushNamed 
FlatButton(onPressed: () => Get.toNamed(page))
//Navigator.pushReplacementNamed 
FlatButton(onPressed: () => Get.offNamed(page))

Previous Articles on GET

State Management using GET

Upcoming Articles

I’ll be writing a list of Articles that will cover almost everything that we can do with GET namingly:

  • Dependency Management
  • Other Fun Helpers/Utilities (SnackBars, Dialog, BottomSheet etc)
  • Architecture using GET

Hope it helps.