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
importStatementtomainDartPathif not already present. -
injectAfterAnchor(
{required String source, required String anchor, required String snippet}) → String -
Insert
snippetimmediately AFTER the closing)of the call expressionanchor(...)insource. The anchor matches the first occurrence of<anchor>((e.g.'Magic.init'matchesawait 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
codeon the line immediately after the closing);ofawait Magic.init(...). -
injectBeforeAnchor(
{required String source, required String anchor, required String snippet, String? indent}) → String -
Insert
snippetimmediately before the line containinganchorinsource. -
injectBeforeMagicInit(
String mainDartPath, String code) → void -
Insert
codeon the line immediately beforeawait 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.