modular_codegen은 FlutterModular용 코드 생성 패키지로, 종속성 주입과 라우트 매개변수 처리를 자동화합니다. @Inject, @Param, @Data와 같은 어노테이션을 사용하여 반복적인 코드를 생성함으로써 수동 설정을 줄이고 타입 안정성을 향상시킵니다. 이는 모듈형 Flutter 애플리케이션의 개발 속도와 유지보수성을 높입니다. 이 패키지는 FlutterModular 생태계와 원활하게 통합되어 모듈 간 효율적인 탐색과 서비스 주입을 가능하게 합니다.
사용 사례
모듈러 Flutter 앱에서 종속성 주입 자동화
@Param 애노테이션을 사용하여 경로 매개변수 생성
서비스 및 컨트롤러 주입 간소화
대규모 Flutter 프로젝트에서 부가 코드 줄이기
모듈 간 유형 안전한 탐색 활성화
주요 기능
주석 기반 코드 생성
@Inject, @Param 및 @Data 지원
FlutterModular과의 원활한 통합
수동 보일러플레이트를 줄입니다
타입 안전성과 성능을 향상시킵니다
적합한 대상
대규모 앱을 위한 FlutterModular 사용 개발자
유지보수 가능한 아키텍처를 우선시하는 팀
자동 종속성 주입이 필요한 프로젝트
복잡한 라우팅 요구 사항을 가진 앱
보일러플레이트를 줄이려는 개발자
고려 사항
FlutterModular을 종속성으로 필요로 합니다
코드 생성을 위해 build_runner가 필요합니다
Dart/Flutter 환경에 한정
빌드 시간을 약간 증가시킬 수 있습니다
주석 및 모듈 패턴에 대한 이해가 필요합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Flutter Modular
A smart, modular project structure
Route management, dependency injection and scoped state — organized by feature, the Flutter way.
Let's find out how to implement a Modular structure in your project.
What is Modular?
Modular proposes to solve two problems:
Modularized routes.
Modularized Dependency Injection.
In a monolithic architecture, where we have our entire application as a single module, we design our software in a quick and
elegant way, taking advantage of all the amazing features of Flutter💙. However, producing a larger app in a "monolithic" way
can generate technical debt in both maintanance and scalability. With this in mind, developers adopted architectural strategies to better divide the code, minimizing the negative impacts on the project's maintainability and scalability..
By better dividing the scope of features, we gain:
Improved understanding of features.
Less breaking changes.
Add new non-conflicting features.
Less blind spots in the project's main business rule.
Improved developer turnover.
With a more readable code, we extend the life of the project. See example of a standard MVC with 3 features(Auth, Home, Product):
What we did in this structure was to continue using MVC, but this time in scope. This means that
each feature has its own MVC, and this simple approach solves many scalability and maintainability issues.
We call this approach "Smart Structure". But two things were still Global and clashed with the structure itself, so we created Modular to solve this impasse.
In short: Modular is a solution to modularize the route and dependency injection system, making each scope have
its own routes and injections independent of any other factor in the structure.
We create objects to group the Routes and Injections and call them Modules.
Ready to get started?
Modular is not only ingenious for doing something amazing like componentizing Routes and Dependency Injections, it's amazing
for being able to do all this simply!
Go to the next topic and start your journey towards an intelligent structure.
Common questions
Does Modular work with any state management approach?
Yes, the dependency injection system is agnostic to any kind of class
including the reactivity that makes up state management.
Can I use dynamic routes?
Yes! The entire route tree responds as on the Web. You can use dynamic :params,
query strings, relative paths, nested routes and persistent shells (RouterOutlet).
Use a route guard to redirect (e.g. to a 404 or a login page).
Do I need to create a Module for all features?
No. You can create a module only when you think it's necessary or when the feature is no longer a part of
the scope in which it is being worked on.
✨ Usage
flutter_modular 7 is a ground-up rewrite. A Module is now exactly the two things that couple a Flutter app — Dependency Injection + Routes — declared with a small functional API. State is page-scoped, tied to the route lifecycle, so ownership and disposal stop being your problem and the durable truth lives in a repository/service registered in DI. Full, runnable demonstrations (nested routes, RouterOutlet shells, route guards, per-module DI lifecycle, arguments/pop-results) live in example/.
Install
dependencies:flutter_modular:^7.0.0
or run flutter pub add flutter_modular.
Declare a module
A module groups routes and dependency injection. Shared dependencies are registered with addSingleton/add*, routes with route(...), and submodules with module(...).
context.pushNamed('/details/42'); // stacks a page (push stays out of the URL)
context.navigate('/'); // replaces the stack (owns the URL, resets history)
context.pop(result); // pops, delivering a result to the awaiting pushNamed
Page-scoped state
State lives 1:1 with a view via provide — built in a page-local scope and disposed when the route leaves, so there are no floating globals and no manual dispose.
c.route('/counter',
provide: (s) => s.addChangeNotifier<CounterViewModel>(CounterViewModel.new),
child: (ctx, state) => constCounterPage(),
);
// inside the page:final vm = context.watch<CounterViewModel>(); // rebuilds when the VM notifies// granular: rebuild only when the selected value changes (provider-style)final count = context.select<CounterViewModel, int>((vm) => vm.count);
🧑💻 Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the appropriate tag.
Don't forget to give the project a star! Thanks again!
Fork the Project
Create your Feature Branch (git checkout -b feature/AmazingFeature)
Commit your Changes (git commit -m 'Add some AmazingFeature')
Push to the Branch (git push origin feature/AmazingFeature)