redirect method

  1. @override
FutureOr<String?> redirect(
  1. BuildContext context,
  2. GoRouterState state
)
override

An optional redirect function for this route.

Subclasses must override one of build, buildPage, or redirect.

Corresponds to GoRoute.redirect.

Implementation

@override
FutureOr<String?> redirect(BuildContext context, GoRouterState state) async {
  if (edit != true) return null;
  // If user attempts to edit, check if the user is the posts author
  final user = ProviderScope.containerOf(context).read(currentUserProvider)!;
  final post = ProviderScope.containerOf(context).read(createPostProvider);
  // If the user is not the author, return to the post
  return (user.uid! == post.userID) ? null : PostRoute(id: id).location;
}