You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 AppController or 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., AbstractServiceOne instead of ServiceOne), @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:

  1. 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 {}
    
  2. Use Flutter community DI packages: Tools like provider, riverpod, or get_it work perfectly with pure Dart classes. For example, with get_it:
    // 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()));
    }
    
    Then access AppController in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:36:22