Using freezed unions as a router for URIs.

Overview

freezed_uri_router

Using freezed objects as routes with URI serialization.

This can be useful when using Navigator V2 APIs with Flutter

Quickstart

Define your routes

The FreezedPath is used to implement Path.fromUri factory and toUri extension method from the root Path type.

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:freezed_uri_router/freezed_uri_router.dart';

part 'path.freezed.dart';
part 'path.g.dart';

final _serializer = const FreezedPath();

@freezed
abstract class Path with _$Path {
  /// `/index`
  const factory Path.index() = IndexRootPath;

  /// `/products/{...}`
  const factory Path.products({
    @required ProductsPath next,
  }) = RootProductsPath;

  factory Path.fromJson(Map<String, dynamic> json) => _$PathFromJson(json);

  factory Path.fromUri(String uri) =>
      Path.fromJson(_serializer.deserialize(uri));
}

extension PathExtension on Path {
  String toUri() => _serializer.serialize(toJson());
}

@freezed
abstract class ProductsPath with _$ProductsPath {

  /// `/products/index?filtering=x`
  const factory ProductsPath.index({
    String filtering,
  }) = IndexProductsPath;

  /// `/products/detail/{...}`
  const factory ProductsPath.detail({
    @required DetailPath next,
  }) = DetailProductsPath;

  factory ProductsPath.fromJson(Map<String, dynamic> json) =>
      _$ProductsPathFromJson(json);
}

@freezed
abstract class DetailPath with _$DetailPath {

  /// `/products/detail/index?id=x`
  const factory DetailPath.index({
    int id,
  }) = IndexDetailPath;

  factory DetailPath.fromJson(Map<String, dynamic> json) =>
      _$DetailPathFromJson(json);
}

Serialize

final uri = Path.products(
  next: ProductsPath.detail(
    next: DetailPath.index(id: 42),
  ),
);

print(uri); // `/products/detail/index?id=i%2442`

Deserialize

final path = Path.fromUri(r'/products/detail/index?id=i%24123');

print(path); // `Path.products(next: ProductsPath.detail(next: DetailPath.index(id: 123)))`

Use path as a router

return path.map(
  index: (path) => IndexPage(),
  products: (path) => path.next.map(
    index: (path) => ProductsPage(filtering: path.filtering),
    detail: (path) => path.next.map(
      index: (path) => ProductDetail(id: path.id),
    )
  ),
);

Serialization

All route properties are serialized with a prefix defining there type.

The only reserved property is next which describes the next segment.

You might also like...

An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization.

AutoMapper for Dart An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization. Examp

Aug 24, 2022

A flutter package with classes to help testing applications using the canvas

Canvas test helpers MockCanvas is a utility class for writing tests for canvas operations. It supports the same API as the regular Canvas class from d

Jan 31, 2022

Volt is a wrapper over the Revolt API for easily writing bots using the Dart language.

Volt is a wrapper over the Revolt API for easily writing bots using the Dart language. It is currently in active development so not all of the functionality has yet been implemented.

Dec 13, 2022

Log snapshot management solution (iOS/Android/Web/Server) built with Flutter/Dart using Bloc pattern and Firebase Firestore backend.

Log snapshot management solution (iOS/Android/Web/Server) built with Flutter/Dart using Bloc pattern and Firebase Firestore backend.

Log snapshot management solution (iOS/Android/Web/Server) built with Flutter/Dart using Bloc pattern and Firebase Firestore backend.

Nov 9, 2022

Provides Dart Build System builder for creating Injection pattern using annotations.

Provides Dart Build System builder for creating Injection pattern using annotations. Gate generator The core package providing generators using annoat

Dec 20, 2022

Barcode scanner using Flutter is the project to sumbit for REVA HACKATHONS

Barcode scanner using Flutter is the project to sumbit for REVA HACKATHONS

QRcode scanner/generator using Flutter Let's build a Generate/Scan QR Codes app with Flutter with which you can share information easily with your mat

Dec 2, 2021

Coneectivity Service is for checking the internet connection using provider

Features Coneectivity Service is for checking the internet connection using provider. Installation Add the latest version of package to your pubspec.y

Nov 28, 2021

A property search app created using flutter

A property search app created using flutter

flutter_property_finder A property listings app built using Flutter sdk. Tutorial Link Watch as i guide you step by step on how to build this applicat

Dec 13, 2022

Some examples for how to build dart code using package:analyzer and package:code_builder only (i.e. no build_runner or build.yaml files)

What is this? Some examples for how to build dart code from another dart code us

Apr 17, 2022
Owner
Aloïs Deniel
Flutter Freelance
Aloïs Deniel
An android application built using Flutter that computes the Body Mass Index of person and suggestion to carry ,by taking Inputs (Weight, Height, and Age), Built using Flutter

BMI Calculator ?? Our Goal The objective of this tutorial is to look at how we can customise Flutter Widgets to achieve our own beautiful user interfa

dev_allauddin 7 Nov 2, 2022
A Dart package to web scraping data from websites easily and faster using less code lines.

Chaleno A flutter package to webscraping data from websites This package contains a set of high-level functions that make it easy to webscrap websites

António Nicolau 30 Dec 29, 2022
Notes app using flutter, firebase and cloud firestore

notes A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this is

Sidheshwar S 8 Aug 10, 2022
A BMI calculator application using Flutter.

BMI Calculator Purpose Maintaining a healthy weight is important for overall health and can help to prevent and control many diseases and conditions.

Srijan Kumar Gupta 3 Apr 21, 2022
Rocket is a parsing framework for parsing binary data structures using efficient parsing algorithms

rocket Version 0.1.10 (BETA) Rocket is a parsing framework for parsing binary data structures using efficient parsing algorithms. Breaking change: The

null 5 Dec 5, 2021
Simple generative arts created using Flutter

Flutter Generative Art Try it out on DartPad Simple Generative Art created using Flutter. Watch the full video on YouTube to know how to build it from

Souvik Biswas 11 Aug 11, 2022
A very basic prototype of macros using build_runner

Description This is a basic prototype for 3 phase macros using package:build. The general idea is that macros run in 3 different phases, and each phas

Jacob MacDonald 64 Dec 14, 2022
An application built using flutter that works as destiny checker, whether our mind wish is as we desire or not with 8 different option.

magic_8_ball_utter A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you s

dev_allauddin 3 Feb 3, 2022
An application built using Flutter that holds a static personal/professional informations related to me in the form of card.(Digital Visiting Card)

mi_card_flutter A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you star

dev_allauddin 3 Feb 3, 2022
Dart port for artwork-extractor using FFI

A simple library to extract & save artwork of a ?? music/audio file. This library is using a artwork-extractor library by alexmercerind. It is super s

null 3 Feb 23, 2022