MainDartEditor class

Targeted mutations for a Flutter application's lib/main.dart file.

All methods are idempotent: calling them a second time with the same arguments leaves the file unchanged. Every method that searches for a Magic.init(...) call throws StateError when the anchor is not present, so callers receive an actionable error rather than silent no-ops.

Usage

// Add an import at the top of main.dart.
MainDartEditor.addImport(
  'lib/main.dart',
  "import 'package:sentry_flutter/sentry_flutter.dart'",
);

// Inject plugin-install code before Magic.init().
MainDartEditor.injectBeforeMagicInit(
  'lib/main.dart',
  '  DuskPlugin.install();\n',
);

// Inject post-init adapters after Magic.init().
MainDartEditor.injectAfterMagicInit(
  'lib/main.dart',
  '  MagicDuskIntegration.install();\n',
);

// Wrap the runApp() call with a higher-order widget.
MainDartEditor.wrapRunApp('lib/main.dart', 'SentryWidget');

// Wrap a custom entry point (e.g. runWidget) instead of runApp.
MainDartEditor.wrapRunApp('lib/main.dart', 'SentryWidget', appCall: 'runWidget');

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

addImport(String mainDartPath, String importStatement) → void
Add importStatement to mainDartPath if not already present.
injectAfterAnchor({required String source, required String anchor, required String snippet}) String
Insert snippet immediately AFTER the closing ) of the call expression anchor(...) in source. The anchor matches the first occurrence of <anchor>( (e.g. 'Magic.init' matches await Magic.init(); the matching closing paren is found via a depth counter so multi-line calls with nested arguments are handled correctly.
injectAfterMagicInit(String mainDartPath, String code) → void
Insert code on the line immediately after the closing ); of await Magic.init(...).
injectBeforeAnchor({required String source, required String anchor, required String snippet, String? indent}) String
Insert snippet immediately before the line containing anchor in source.
injectBeforeMagicInit(String mainDartPath, String code) → void
Insert code on the line immediately before await Magic.init(...).
wrapRunApp(String mainDartPath, String wrapperName, {String appCall = 'runApp'}) → void
Replace <appCall>(<expr>) with <appCall>(<wrapperName>(<expr>)).
wrapRunAppInSource(String source, String wrapperName, {String appCall = 'runApp', String sourceName = '<source>'}) String
Pure-functional sibling of wrapRunApp.