Middleware
Depends on
routes.dart
and pages/not_found.dart (You need to create a new page if you are not extending this whole repo).Route middleware is used to check if route exists or not, and show the page accordingly.
As of now we have middleware.dart
file in routes directory
import 'package:flutter/material.dart';
import './routes.dart';
import '../views/pages/not_found.dart';
Route<dynamic>? routeMiddleware(RouteSettings route) {
if (!routes.containsKey(route.name)) {
return NotFoundPage.route();
}
return routes[route.name]!();
}
If you're not using the whole downloaded repository and you are using few modules only, then to use routeMiddleware
in your app, you need to assign routeMiddleware
to onGenerateRoute
.
MaterialApp(
...
onGenerateRoute: routeMiddleware,
...
);
if you need central permission handler then you can implement it in middleware. check below example:
Note:
Each project will need to configure permission handling mechanism saperatly because every project might have different End points, Asset Controllers, User types. the following example is a demo for Vaah permission handling.
Each project will need to configure permission handling mechanism saperatly because every project might have different End points, Asset Controllers, User types. the following example is a demo for Vaah permission handling.
import 'package:flutter/material.dart';
import 'package:example/models/user.dart';
import 'package:example/routes/routes.dart';
import 'package:example/view/pages/not_found.dart';
import 'package:example/view/pages/permission_denied.dart';
Route<dynamic>? routeMiddleware(RouteSettings route) {
if (!routes.containsKey(route.name)) {
return NotFoundPage.route();
}
if (!User.hasPermission('can-access-${route.name}')) {
return PermissionDeniedPage.route();
}
return routes[route.name]!();
}
The same thing can be implemented in each page individually (in their route methods, check the example below). So permission will be checked everytime when we push the route. Also, please keep above note in mind.
home.dart
class DetailsPage extends StatefulWidget {
static const String routeName = '/details';
static Route<void> route() {
if (!User.hasPermission('can-access-details-page')) {
return PermissionDenied.route();
}
return MaterialPageRoute(
settings: const RouteSettings(name: '/details'),
builder: (_) => const DetailsPage(),
);
}
...