Mobile Development 12 min read

How to Implement AOP in Flutter with AspectD: A Complete Guide

This article explains the challenges of applying aspect‑oriented programming in Flutter, introduces the open‑source AspectD framework for Dart, and walks through its design, supported syntax, code examples, and integration steps for both debug and release builds.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
How to Implement AOP in Flutter with AspectD: A Complete Guide

Problem Background

When implementing an automated recording and playback system for Flutter, we discovered that modifying the Flutter framework (Dart layer) was required, which introduced invasive changes and increased maintenance costs. To reduce this invasiveness, we turned to aspect‑oriented programming (AOP) as a primary solution.

AspectD: An AOP Framework for Dart

AspectD is an open‑source AOP framework for Dart, published on GitHub. It enables developers to write AOP code that can be compiled together with the original project, allowing injection of cross‑cutting concerns without modifying the original source directly.

Design Overview

1. Design Diagram

2. Typical AOP Scenario

The following code demonstrates a typical AOP usage with AspectD:

import 'package:example/main.dart' as app;
import 'aop_impl.dart';

void main() => app.main();

@Aspect()
@pragma("vm:entry-point")
class ExecuteDemo {
  @Execute("package:example/main.dart", "_MyHomePageState", "-_incrementCounter")
  @pragma("vm:entry-point")
  void _incrementCounter(PointCut pointcut) {
    pointcut.proceed();
    print('KWLM called!');
  }
}

3. API Design for Developers

AspectD defines a PointCut class that captures the target library, class, method, and parameter information needed for AOP logic. It also uses @pragma('vm:entry-point') annotations to prevent tree‑shaking from discarding AOP code during AOT compilation.

@pragma('vm:entry-point')
class PointCut {
  final Map<dynamic, dynamic> sourceInfos;
  final Object target;
  final String function;
  final String stubId;
  final List<dynamic> positionalParams;
  final Map<dynamic, dynamic> namedParams;

  @pragma('vm:entry-point')
  PointCut(this.sourceInfos, this.target, this.function, this.stubId, this.positionalParams, this.namedParams);

  @pragma('vm:entry-point')
  Object proceed() {
    return null;
  }
}

4. Advice Design

@pragma('vm:entry-point')
Future<String> getCurTime(PointCut pointcut) async {
  // ...
  return result;
}

5. Aspect Design

@Aspect()
@pragma('vm:entry-point')
class ExecuteDemo {
  @pragma('vm:entry-point')
  ExecuteDemo();
  // ...
}

6. Supported Syntax

AspectD provides three main annotations: @Call: injects code at the call point. @Execute: injects code at the execution point. @Inject: allows insertion of custom logic at a specific line number within a method.

@Aspect()
@pragma('vm:entry-point')
class CallDemo {
  @Call('package:app/calculator.dart', 'Calculator', '-getCurTime')
  @pragma('vm:entry-point')
  Future<String> getCurTime(PointCut pointcut) async {
    print('Aspectd:KWLM02');
    var result = pointcut.proceed();
    return result;
  }
}

7. Build Process Support

Standard Flutter tooling does not support compiling AOP code together with the original project. By applying a custom patch to flutter_tools, AspectD can be integrated into the build pipeline, enabling both debug and release builds to include AOP transformations.

git apply -3way /path/to/aspectd.patch
rm bin/cache/flutter_tools.stamp
flutter doctor -v

Practical Impact

Using AspectD, the team eliminated invasive modifications to the Flutter framework while achieving the same functionality, supporting hundreds of scripts for recording, playback, and automated regression testing. The framework also enables performance tracing, enhanced logging, and complex scenario automation through its flexible AOP capabilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DARTFlutterAspectDaop
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.