Last active
June 1, 2023 07:38
-
-
Save kenresoft/f0e2c2bd00001526233a3ddcd1210865 to your computer and use it in GitHub Desktop.
Sample of my GoRouter setup methods in my main.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
WidgetsFlutterBinding.ensureInitialized(); | |
SystemChrome.setPreferredOrientations([ | |
DeviceOrientation.portraitUp, | |
DeviceOrientation.portraitDown, | |
DeviceOrientation.landscapeRight, | |
DeviceOrientation.landscapeLeft, | |
]).then(appCallback); | |
} | |
class MyApp extends StatelessWidget { | |
MyApp({Key? key}) : super(key: key); | |
// build method here ... | |
final GoRouter _router = GoRouter( | |
routes: <GoRoute>[ | |
route(Constant.root, const Dashboard()), | |
route(Constant.dashboard, const Dashboard()), | |
], | |
errorBuilder: (context, state) => const ErrorPage(), | |
); | |
} | |
GoRoute route(String path, Widget route) { | |
return GoRoute( | |
path: path, | |
builder: (BuildContext context, GoRouterState state) => route, | |
); | |
} | |
FutureOr appCallback(void value) { | |
runApp( | |
ProviderScope( | |
child: MyApp(), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample of my GoRouter setup methods in my main.dart
The code defines three functions: main, route, and appCallback.
The
main
function creates a GoRouter object and passes it two routes and an error builder. The two routes areConstant.root
andConstant.dashboard
, which are string constants that represent the root and dashboard paths respectively.The Dashboard page is associated with both routes. The
errorBuilder
is a function that takes a BuildContext and a GoRouterState and returns an ErrorPage page.The
route
function takes a path and a route page as arguments and returns a GoRoute object. The GoRouteobject is created with the path and a builder function that takes a BuildContext and a GoRouterState and returns the route widget.
The
appCallback
function takes a value argument and runs a Flutter app. The app is wrapped in a ProviderScope widget, which provides a Provider for the app.The
MyApp
widget is the root widget of the app.