AngularDart与Flutter共享代码:AppController依赖注入疑问
Hey there! Let's break down your questions step by step since you're working on a shared codebase between AngularDart and Flutter—smart move reusing business logic across frameworks!
AngularDart: Do you need @Injectable()?
Short answer: It's strongly recommended, especially for classes with constructor dependencies like your AppController.
AngularDart's DI system can resolve simple classes without @Injectable() in some cases, but marking your controllers and services with @Injectable() eliminates edge cases and makes your intent clear. For example:
- If your
AppControlleror services have multiple constructors, Angular needs@Injectable()to know which one to use for injection. - If you ever swap a concrete service with an abstract class (e.g.,
AbstractServiceOneinstead ofServiceOne),@Injectable()becomes mandatory to help DI resolve the implementation.
Here's how your shared AppController should look for AngularDart:
import 'package:angular/angular.dart'; @Injectable() class AppController { final ServiceOne serviceOne; final ServiceTwo serviceTwo; // Constructor injection works seamlessly with @Injectable() AppController(this.serviceOne, this.serviceTwo); } // Don't forget to mark your services too! @Injectable() class ServiceOne {} @Injectable() class ServiceTwo {}
Then register them in your AngularDart bootstrap:
void main() { bootstrap(AppComponent, [ AppController, ServiceOne, ServiceTwo, ]); }
Flutter: Handling DI for shared code
You're right—Flutter doesn't have a built-in DI system like AngularDart, but that doesn't mean you can't reuse your shared AppController and services. The key is to keep your shared code framework-agnostic.
Best practice: Keep shared classes "pure"
Avoid adding AngularDart-specific annotations (like @Injectable()) directly to your shared code if you can. Instead, use one of these approaches:
- Conditional imports: Only apply
@Injectable()when compiling for AngularDart. Example:// shared/app_controller.dart class AppController { final ServiceOne serviceOne; final ServiceTwo serviceTwo; AppController(this.serviceOne, this.serviceTwo); } // angular/app_controller.injectable.dart import 'package:angular/angular.dart'; import '../shared/app_controller.dart'; @Injectable() class AppController extends SharedAppController {} - Use Flutter community DI packages: Tools like
provider,riverpod, orget_itwork perfectly with pure Dart classes. For example, withget_it:
Then access// Flutter setup final getIt = GetIt.instance; void setupDI() { // Register singleton instances of your services getIt.registerSingleton<ServiceOne>(ServiceOne()); getIt.registerSingleton<ServiceTwo>(ServiceTwo()); // Inject dependencies into AppController getIt.registerSingleton<AppController>(AppController(getIt(), getIt())); }AppControllerin your Flutter widgets:final appController = getIt<AppController>();
Final takeaways
- For AngularDart: Always mark injectable classes with
@Injectable()to avoid DI headaches down the line. - For cross-framework compatibility: Keep your core business logic (controllers, services) as pure Dart classes with no framework-specific dependencies.
- Flutter DI: Use community packages to wire up your shared classes—they're lightweight and play nicely with pure Dart code.
内容的提问来源于stack exchange,提问作者Augie Li

