create a class extends StatefulWidget, and add a function, then occur CompileError
test case:
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eval/flutter_eval.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('test MyWidget function', (WidgetTester tester) async {
final compiler = Compiler();
setupFlutterForCompile(compiler);
final program = compiler.compile({
'example': {
'main.dart': '''
import 'package:flutter/material.dart';
class MyWidget extends StatefulWidget {
void Function(int) _callback;
MyWidget(this._callback);
void testFun() {
print('testFun');
return _callback(1);
}
@override
State<MyWidget> createState() {
return MyWidgetState();
}
}
class MyWidgetState extends State<MyWidget> {
MyWidgetState();
final TextEditingController controller = TextEditingController();
String text = 'Bazinga';
@override
void initState() {
super.initState();
widget.testFun();
controller.addListener(() {
setState(() {
text = controller.text + '123';
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(body: Column(children: [
TextField(
controller: controller,
),
Text(text)
])));
}
}
'''
}
});
final runtime = Runtime(program.write().buffer.asByteData());
setupFlutterForRuntime(runtime);
await tester.pumpWidget(
runtime.executeLib('package:example/main.dart', 'MyWidget.', [
$Closure((runtime, target, args) {
debugPrint('eval args ${args[0]!.$value}');
return null;
}),
]));
await tester.enterText(find.byType(TextField), 'Hello');
await tester.pump();
expect(find.text('Hello123'), findsOneWidget);
});
}
Error: ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ The following CompileError was thrown running a test: Unknown method StatefulWidget.testFun at "widget.testFun()" (file package:example/main.dart)
When the exception was thrown, this was the stack:
#0 resolveInstanceMethod (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:350:9)
#1 resolveInstanceMethod (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:355:14)
#2 resolveInstanceMethod (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:355:14)
#3 resolveInstanceMethod (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:355:14)
#4 _invokeWithTarget (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:259:12)
#5 compileMethodInvocation (package:dart_eval/src/eval/compiler/expression/method_invocation.dart:55:12)
#6 compileExpression (package:dart_eval/src/eval/compiler/expression/expression.dart:38:12)
#7 compileExpressionAndDiscardResult (package:dart_eval/src/eval/compiler/expression/expression.dart:106:12)
#8 compileStatement (package:dart_eval/src/eval/compiler/statement/statement.dart:26:15)
#9 compileBlock (package:dart_eval/src/eval/compiler/statement/block.dart:19:20)
#10 compileMethodDeclaration (package:dart_eval/src/eval/compiler/declaration/method.dart:56:14)
#11 compileDeclaration (package:dart_eval/src/eval/compiler/declaration/declaration.dart:21:12)
#12 compileClassDeclaration (package:dart_eval/src/eval/compiler/declaration/class.dart:47:5)
#13 compileDeclaration (package:dart_eval/src/eval/compiler/declaration/declaration.dart:17:5)
#14 Compiler.compileSources.
same issue with controller. happens when you set the contollers text property