NavigationCrossroad<T> constructor

NavigationCrossroad<T>({
  1. required T activeBranch,
  2. Map<T, NavigationStack<T>>? availableBranches,
  3. GlobalKey<NavigatorState>? navigatorKey,
  4. Map<T, GlobalKey<NavigatorState>>? navigatorKeys,
  5. Object? branchParam,
  6. Map<T, Object?>? branchParams,
})

Implementation

NavigationCrossroad({
  required this.activeBranch,
  Map<T, NavigationStack<T>>? availableBranches,
  GlobalKey<NavigatorState>? navigatorKey,
  Map<T, GlobalKey<NavigatorState>>? navigatorKeys,
  this.branchParam,
  Map<T, Object?>? branchParams,
})  : availableBranches = UnmodifiableMapView(availableBranches ?? {}),
      navigatorKey = navigatorKey ?? GlobalKey<NavigatorState>() {
  this.branchParams = UnmodifiableMapView(
    branchParams != null
        ? {
            ...branchParams,
            ...Map.fromEntries(this
                .availableBranches
                .keys
                .toSet()
                .difference(branchParams.keys.toSet())
                .map((e) => MapEntry(e, null))),
          }
        : Map.fromEntries(
            this.availableBranches.keys.map((e) => MapEntry(e, null))),
  );

  assert(() {
    if (this.branchParams.length != this.availableBranches.length) {
      throw NavigationStackError(
          'branchParams list needs to have the size equal to the branches count');
    }
    return true;
  }());

  this.navigatorKeys = UnmodifiableMapView(
    navigatorKeys != null
        ? {
            ...navigatorKeys,
            ...Map.fromEntries(this
                .availableBranches
                .keys
                .toSet()
                .difference(navigatorKeys.keys.toSet())
                .map((e) => MapEntry(e, GlobalKey<NavigatorState>()))),
          }
        : Map.fromEntries(
            this.availableBranches.keys.map(
                  (e) => MapEntry(e, GlobalKey<NavigatorState>()),
                ),
          ),
  );

  assert(() {
    if (!setEquals(this.navigatorKeys.keys.toSet(),
        this.availableBranches.keys.toSet())) {
      throw NavigationStackError(
          'There are missing keys for in the navigatorKeys. '
          'It has to match the same branches as in availableBranches.');
    }
    return true;
  }());
}