Skip to content

Instantly share code, notes, and snippets.

@CodyDunlap
Last active December 9, 2019 04:52
Show Gist options
  • Save CodyDunlap/8500589c5d846f84698593ba39f9afc6 to your computer and use it in GitHub Desktop.
Save CodyDunlap/8500589c5d846f84698593ba39f9afc6 to your computer and use it in GitHub Desktop.
Injecting services into Widget's using package:inject
@Injector(const [ServicesModule, ViewModelModule])
abstract class AppComponent {
static Future<AppComponent> create(
ServicesModule servicesModule,
ViewModelModule viewModule,
) async {
return await g.AppComponent$Injector.create(servicesModule, viewModule);
}
@provide
MyApp get app;
}
typedef Provider<T> = T Function();
void main() async {
var container = await AppComponent.create(ServicesModule(), ViewModelModule());
runApp(container.app);
}
@provide
class MyApp extends StatelessWidget {
final Router _router;
MyApp(this._router);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
initialRoute: '/',
onGenerateRoute: _router.generateRoute
);
}
}
@provide
class Router {
final Provider<HomeView> _homePage;
final Provider<LoginView> _loginPage;
final Provider<SettingsView> _settingsPage;
Router(
this._homePage,
this._loginPage,
this._settingsPage,
);
Route<dynamic> generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => _homePage());
case '/login':
return MaterialPageRoute(builder: (_) => _loginPage());
case '/settings':
return MaterialPageRoute(builder: (_) => _settingsPage());
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}')),
));
}
}
}
@module
class ServicesModule {
@provide
@singleton
SomeService provideSomeService() => SomeService();
}
class SettingsView extends StatefulWidget {
SettingsView({Key key, this.someService}) : super(key: key);
final SomeService someService;
@override
_SettingsViewState createState() => _SettingsViewState();
}
class _SettingsViewState extends State<SettingsView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Settings"),
),
body: Center(
child: Text("Settings"),
),
);
}
}
@module
class ViewModule {
@provide
MyWidgetView provideMyWidgetView(SomeService someService) => MyWidgetView({someService: someService});
}
@xsahil03x
Copy link

Hey @CodyDunlap, can you tell me what are those providers in router.dart?

@CodyDunlap
Copy link
Author

I added the app_module.dart file so you can see what the Provider is. It's just a function so that we can inject it into a class and only create an instance of the dependency when needed.

@xsahil03x
Copy link

Hey @CodyDunlap thanks for so much help, It is working now just one question remaining. How will you pass params in this kind of navigation? Do we need to create separate typedefs for separate screens?

@div3791
Copy link

div3791 commented Dec 9, 2019

Hey @CodyDunlap thanks for so much help, It is working now just one question remaining. How will you pass params in this kind of navigation? Do we need to create separate typedefs for separate screens?

let me know if you got the solution for this. I am also stucked at this point

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