0 purchases
flutter middleware
Installation #
flutter_middleware: current_version
copied to clipboard
Initial full code #
import 'package:flutter_middleware/flutter_middleware.dart';
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
Middleware.state.set(
$middlewares: [ExampleMiddleware()], // all global middleware here
$navigatorKey: navigatorKey,
);
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey, // <<<<<-------- add this
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: '),
),
),
);
}
}
copied to clipboard
Using #
create folder and file middleware/example_middleware.dart
import 'package:flutter_middleware/flutter_middleware.dart';
class ExampleMiddleware extends FlutterMiddleware{
@override
Future<bool> check(RequestData data) async {
// do logic here
return true;
}
@override
approved(RequestData data) {
// approved
return super.approved(data);
}
@override
denied() {
print('middleware example denied');
return super.denied();
}
}
copied to clipboard
create folder and file controller/example_controller.dart
class ExampleController extends Controllers{
static final state = ExampleController._state();
ExampleController._state(){
use for custom only this controller
// addMiddleware([]);
}
checkDataUser() async {
return 'success running';
}
}
copied to clipboard
simple implementation in view ontap()
GestureDetector(
onTap: ()async {
final controller = ExampleController.state;
final e = await controller.call(controller.checkDataUser,context: context); // context is nullable
},
child: Center(
child: Text('Running on: '),
),
)
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.