ethanblake4 / flutter_eval

Code push for Flutter, powered by dart_eval
https://pub.dev/packages/flutter_eval
BSD 3-Clause "New" or "Revised" License
301 stars 28 forks source link

create a class extends StatefulWidget, and add a function, then occur CompileError #72

Open HXiaoMing opened 6 months ago

HXiaoMing commented 6 months ago

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.. (package:dart_eval/src/eval/compiler/compiler.dart:493:11)

15 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)

16 Compiler.compileSources. (package:dart_eval/src/eval/compiler/compiler.dart:482:15)

17 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:633:13)

18 Compiler.compileSources (package:dart_eval/src/eval/compiler/compiler.dart:478:32)

19 Compiler.compile (package:dart_eval/src/eval/compiler/compiler.dart:164:12)

20 main. (file:///Users/tt/tech/flutter/flutterPro/flutter_eval/test/custom/widget/widget_test.dart:11:30)

21 testWidgets.. (package:flutter_test/src/widget_tester.dart:168:29)

#22 TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:1013:5) (elided one frame from package:stack_trace)
JeffOlajos commented 3 weeks ago

same issue with controller. happens when you set the contollers text property