Skip to content

Instantly share code, notes, and snippets.

@ulusoyca
Last active January 23, 2023 18:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulusoyca/4efc0db8605f3055f69c2a46fe6ef9d0 to your computer and use it in GitHub Desktop.
Save ulusoyca/4efc0db8605f3055f69c2a46fe6ef9d0 to your computer and use it in GitHub Desktop.
NavigatorWithAuth
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
MyAppRouterDelegate delegate;
AuthRepository authRepository;
@override
void initState() {
delegate = MyAppRouterDelegate(AuthRepository(Preference()));
authRepository = AuthRepository(Preference());
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<AuthViewModel>(
create: (_) => AuthViewModel(authRepository),
child: MaterialApp(home: Router(routerDelegate: delegate)),
);
}
}
@eximius313
Copy link

Why duplicate creation of the instances, and instead:

  @override
  void initState() {
    delegate = MyAppRouterDelegate(AuthRepository(Preference()));
    authRepository = AuthRepository(Preference());
    super.initState();
  }

write

  @override
  void initState() {
    var preference = Preference()
    authRepository = AuthRepository(preference);
    delegate = MyAppRouterDelegate(authRepository);
    super.initState();
  }

?

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