DEV Community

Cover image for Forget GoRouter Boilerplate: This Flutter Package Creates Entire Apps in One Command
Mukhbit
Mukhbit

Posted on

Forget GoRouter Boilerplate: This Flutter Package Creates Entire Apps in One Command

Introducing Go Router Sugar: A “Zero-Ambiguity” routing solution with Smart Parameter Detection, Zero-Config Guards, and a CLI that will change your workflow forever.

If you’ve built a Flutter app of any significant size, you know the routing dance. You love the power of go_router, but you also feel the pain of its verbose, string-based, and error-prone setup.

You write dozens of GoRoute configurations. You manually parse path and query parameters, carefully checking for nulls and converting strings to integers. You create complex redirect logic. And with every new page, the boilerplate grows.

What if we could eliminate all of it? Not just some of it—all of it.

That’s the question that led me to create Go Router Sugar 🍬, a new Flutter package built on a simple but powerful "Zero-Ambiguity" philosophy: the correct way to handle routing should also be the easiest way.

The Zero-Ambiguity Advantage

Go Router Sugar isn't just another helper; it's a complete rethinking of the routing workflow. It introduces several revolutionary features designed to eliminate confusion and boilerplate.

Let’s look at the Before and After.

❌ Before: The Manual Grind

This is the code we’ve all written. It’s fragile, verbose, and disconnected from our page logic.

// 50+ lines of repetitive GoRoute configuration
GoRouter(
  routes: [
    GoRoute(
      path: '/products/:id',
      builder: (context, state) {
        // Manually parse everything, risking runtime errors
        final id = state.pathParameters['id']!; 
        final category = state.uri.queryParameters['category'];
        final pageStr = state.uri.queryParameters['page'];
        final page = pageStr != null ? int.tryParse(pageStr) : null;

        return ProductPage(id: id, category: category, page: page);
      },
    ),
    // ...endless boilerplate for every route
  ],
);

// String-based navigation (typo = crash!)
context.go('/prodcuts/123?category=electronics'); // Oops! Typo = runtime error
Enter fullscreen mode Exit fullscreen mode

✅ After: Zero-Ambiguity Magic

With Go Router Sugar, your routing configuration lives where it belongs: in your page’s constructor.

// 🎯 Your constructor IS your route config. No setup needed.
class ProductPage extends StatelessWidget {
  final String productId;    // Auto-becomes /products/:productId  
  final String? category;    // Auto-becomes ?category=value
  final int? page;          // Auto-parsed from ?page=1

  const ProductPage({
    super.key,
    required this.productId,  // Required = Path parameter
    this.category,            // Optional = Query parameter  
    this.page,               // Nullable = Optional query
  });
  // ... build method
}

// 🎯 100% type-safe navigation
Navigate.goToProduct(
  productId: '123',         // ✅ Required - impossible to forget!
  category: 'electronics',  // ✅ Optional - perfect IntelliSense
  page: 2,                 // ✅ Type-safe int - no parsing errors
);
Enter fullscreen mode Exit fullscreen mode

This is the core principle: Smart Parameter Detection. Required parameters become path parameters. Optional/nullable parameters become query parameters. String, int, bool, and double are all parsed automatically. No configuration, no manual parsing, no boilerplate.

But Wait, There's More: Instant App Creation ⚡

The "Zero-Ambiguity" philosophy extends to the entire project setup. Go Router Sugar comes with a powerful CLI that can generate complete, production-ready Flutter applications in seconds.

Ever wanted to spin up a fully functional e-commerce app to test a feature? Now you can.

# Create a complete Flutter e-commerce app in one command
dart run go_router_sugar new my_app --template ecommerce

cd my_app && flutter run # Your app is running!
Enter fullscreen mode Exit fullscreen mode

This single command scaffolds a multi-page application with products, a shopping cart, a profile, and a complete authentication flow—all powered by Go Router Sugar.

Available Templates:

  • minimal - A clean starter with basic navigation.
  • ecommerce - A full-featured shopping app.
  • auth - A complete authentication flow with login, signup, and protected routes.

Effortless Security: Zero-Config Route Guards 🛡️

Protecting routes shouldn't require complex redirect logic inside your router instance. With Go Router Sugar, it’s a simple interface.

  1. Implement the RouteGuard interface:
// ✅ Simple interface implementation = Instant auth protection
class AuthGuard implements RouteGuard {
  @override
  bool canAccess(BuildContext context, GoRouterState state) {
    // Your authentication logic here
    return UserService.isLoggedIn;
  }

  @override
  String get redirectPath => '/login'; // Where to send unauthenticated users
}
Enter fullscreen mode Exit fullscreen mode
  1. Annotate your page:
// ✅ One annotation = Protected route
@Protected([AuthGuard])
class ProfilePage extends StatelessWidget {
  // This page is now automatically protected!
}
Enter fullscreen mode Exit fullscreen mode

That’s it. No central configuration, no manual checks. Your protection logic lives with your business logic, and your routes are declarative.

Getting Started

Ready to try it?

🚀 For a new project (Recommended):
The fastest way to experience Go Router Sugar is to let it build an app for you.

dart run go_router_sugar new my_app --template minimal
cd my_app
flutter run
Enter fullscreen mode Exit fullscreen mode

🔧 For an existing project:

  1. Add the dependencies to your pubspec.yaml:
dependencies:
  go_router: ^16.1.0

dev_dependencies:
  build_runner: ^2.4.9
  go_router_sugar: ^1.1.0
Enter fullscreen mode Exit fullscreen mode
  1. Organize your pages in the lib/pages directory. The folder structure will become your route map.
  2. Run the generator:
dart run go_router_sugar generate
Enter fullscreen mode Exit fullscreen mode
  1. Update your MaterialApp.router to use the generated AppRouter.router.

A New Philosophy for Flutter Development

Go Router Sugar is more than a collection of features. It’s a push towards a better developer experience—one where our tools are so intuitive they become invisible. By embracing convention over configuration, we can eliminate entire classes of bugs, reduce code complexity, and ultimately, build better apps faster.

I invite you to try it out. Make routing fun again.


You can find the package on Pub.dev and the source code on *GitHub*. Stars are greatly appreciated! Feel free to open an issue or start a discussion with your feedback.

Top comments (0)