riverpod_navigator_core 0.29.0 copy "riverpod_navigator_core: ^0.29.0" to clipboard
riverpod_navigator_core: ^0.29.0 copied to clipboard

outdated

Simple but powerfull Flutter navigation with riverpod, freezed and Navigator 2.0.

example/main.dart

import 'dart:async';

import 'package:meta/meta.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_navigator_core/riverpod_navigator_core.dart';

class HomeSegment extends TypedSegment {
  const HomeSegment();
  // ignore: avoid_unused_constructor_parameters
  factory HomeSegment.fromUrlPars(UrlPars pars) => HomeSegment();
}

@immutable
class BookSegment extends TypedSegment {
  const BookSegment({required this.id});
  factory BookSegment.fromUrlPars(UrlPars pars) => BookSegment(id: pars.getInt('id'));
  final int id;
  @override
  void toUrlPars(UrlPars pars) => pars.setInt('id', id);
}

class LoginSegment extends TypedSegment {
  const LoginSegment();
  // ignore: avoid_unused_constructor_parameters
  factory LoginSegment.fromUrlPars(UrlPars pars) => LoginSegment();
}

final routes = <RRoute4Dart>[
  RRoute4Dart<HomeSegment>(HomeSegment.fromUrlPars),
  RRoute4Dart<BookSegment>(BookSegment.fromUrlPars),
  RRoute4Dart<LoginSegment>(LoginSegment.fromUrlPars),
];

final loginProvider = StateProvider<bool>((_) => false);

class TestNavigator extends RNavigatorCore {
  TestNavigator(Ref ref, {this.delayMsec, this.isError = false}) : super(ref, []);

  final int? delayMsec;
  final bool isError;

  @override
  FutureOr<TypedPath> appNavigationLogicCore(TypedPath oldNavigationStack, TypedPath ongoingPath) {
    if (delayMsec == null) {
      if (isError) throw 'SYNC ERROR';
      return ongoingPath;
    } else {
      return Future.delayed(Duration(milliseconds: delayMsec!)).then<TypedPath>((value) {
        if (isError) throw 'ASYNC ERROR';
        return ongoingPath;
      });
    }
  }
}

Future main() async {
  final container = ProviderContainer(
      overrides: RNavigatorCore.providerOverrides(
    [HomeSegment()],
    TestNavigator.new,
  ));
  final navigator = container.read(navigatorProvider);

  await container.pump();
  await navigator.navigationCompleted;
  final p1 = navigator.navigationStack2Url;
  assert(p1 == '{"runtimeType":"HomeSegment"}');

  container.read(ongoingPathProvider.notifier).state = [HomeSegment(), BookSegment(id: 1)];
  await container.pump();
  await navigator.navigationCompleted;
  final p2 = navigator.navigationStack2Url;
  assert(p2 == '{"runtimeType":"HomeSegment"}/{"runtimeType":"BookSegment","id":1}');
  return;
}
7
likes
0
pub points
18%
popularity

Publisher

unverified uploader

Simple but powerfull Flutter navigation with riverpod, freezed and Navigator 2.0.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

json_annotation, meta, riverpod, tuple

More

Packages that depend on riverpod_navigator_core