navarggen

A source code generator for Flutter route navigation arguments.

Getting Started

Add the packages to your pubspec.yaml file:

dependencies:
  navarggen: ^[latest version]

dev_dependencies:
  build_runner: ^1.5.2 # Also depend on build_runner if you aren't already.
  navarggen_generator: ^[latest version]

Annotate your Widget class with NavigationArguments:

@NavigationArguments({
  'argumentName': ArgumentType,
  'title': String,
  'numbers': 'List<int>', // When you can't pass a Type, you can pass a String. 
})
class YourPage extends StatefulWidget { //...

Specify the part file for the generator code:

part 'your_widget_file_name.g.dart';

Run the build:

$ flutter pub run build_runner build

...or have it watch for changes:

$ flutter pub run build_runner watch

The output will be the following code in the your_widget_file_name.g.dart file:

class YourPageArgs {
  final ArgumentType argumentName;
  final String title;
  final List<int> numbers;
  
  YourPageArgs({this.argumentName, this.title, this.numbers});
}

You can then use it to pass arguments to your route like usual:

Navigator.of(context).pushNamed(
  Routes.YOUR_PAGE,
  arguments: YourPageArgs({
    argumentName: someObject,
    title: 'Your Title',
    numbers: [0, 1, 2, 3, 4],
  }),
);

Then in YourPage:

YourPageArgs args = ModalRoute.of(context).settings.arguments;

See the example project for a working example.

Libraries