Skip to content

Instantly share code, notes, and snippets.

@kenresoft
Last active June 1, 2023 07:38
Show Gist options
  • Save kenresoft/f0e2c2bd00001526233a3ddcd1210865 to your computer and use it in GitHub Desktop.
Save kenresoft/f0e2c2bd00001526233a3ddcd1210865 to your computer and use it in GitHub Desktop.
Sample of my GoRouter setup methods in my main.dart
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(),
),
);
}
@kenresoft
Copy link
Author

kenresoft commented May 30, 2023

Sample of my GoRouter setup methods in my main.dart

The code defines three functions: main, route, and appCallback.

  1. The main function creates a GoRouter object and passes it two routes and an error builder. The two routes are
    Constant.root and Constant.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.

  2. The route function takes a path and a route page as arguments and returns a GoRoute object. The GoRoute
    object is created with the path and a builder function that takes a BuildContext and a GoRouterState and returns the route widget.

  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment