Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsEvalResult.isPromise is wrong #155

Open
zhouxiaofu opened this issue Jul 18, 2024 · 0 comments
Open

jsEvalResult.isPromise is wrong #155

zhouxiaofu opened this issue Jul 18, 2024 · 0 comments

Comments

@zhouxiaofu
Copy link

zhouxiaofu commented Jul 18, 2024

PS E:\flutter\js_demo> flutter --version
Flutter 3.22.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 761747bfc5 (6 weeks ago) • 2024-06-05 22:15:13 +0200
Engine • revision edd8546116
Tools • Dart 3.4.3 • DevTools 2.34.3

flutter_js version: flutter_js: ^0.8.1

environment

  • win11
  • Android

Click the test async button to print:

I/flutter (15640): test async: Instance of 'Future<dynamic>' false

This is wrong. jsEvalResult.isPromise is wrong.

Click the test async2 button to print the content correctly

Demo

import 'package:flutter/material.dart';
import 'package:flutter_js/flutter_js.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late JavascriptRuntime javascriptRuntime;

  @override
  void initState() {
    super.initState();
    javascriptRuntime = getJavascriptRuntime();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            FilledButton(
                onPressed: () {
                  JsEvalResult jsEvalResult = javascriptRuntime.evaluate("2 + 3");
                  debugPrint('test: ${jsEvalResult.stringResult} ${jsEvalResult.isError}');
                },
                child: const Text("test")),
            FilledButton(
                onPressed: () async {
                  JsEvalResult jsEvalResult = javascriptRuntime.evaluate("""
                  (async function() {
                    const promise = new Promise((resolve, reject) => {
                        setTimeout(() => {
                            try {
                                resolve("async ok");
                            } catch (error) {
                                reject(error);
                            }
                        }, 2000);
                    });
                    const result = await promise;
                    console.log("result", typeof result, result);
                    return result;
                   })();
                  """);
                  if (jsEvalResult.isPromise) {
                    jsEvalResult = await javascriptRuntime.handlePromise(jsEvalResult);
                  }
                  debugPrint('test async: ${jsEvalResult.stringResult} ${jsEvalResult.isError}');
                },
                child: const Text("test async")),
            FilledButton(
                onPressed: () async {
                  JsEvalResult jsEvalResult = javascriptRuntime.evaluate("""
                  (async function() {
                    const promise = new Promise((resolve, reject) => {
                        setTimeout(() => {
                            try {
                                resolve("async ok");
                            } catch (error) {
                                reject(error);
                            }
                        }, 2000);
                    });
                    const result = await promise;
                    console.log("result", typeof result, result);
                    return result;
                   })();
                  """);
                  if (jsEvalResult.isPromise || jsEvalResult.stringResult == "Instance of 'Future<dynamic>'") {
                    jsEvalResult = await javascriptRuntime.handlePromise(jsEvalResult);
                  }
                  debugPrint('test async: ${jsEvalResult.stringResult} ${jsEvalResult.isError}');
                },
                child: const Text("test async2"))
          ],
        ),
      ),
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant