Search code examples
flutterdartdart-pubdart-html

flutter library dart:html using eval from javascript


I need js method eval() in Flutter Dart code. How I can get it? I try import

import 'dart:html';
import 'dart:js';

But I still do not have eval() method. Any idea how to import this method? I need evaluate expression like in this js sample: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_eval but in Dart code, so I try import this eval() function from dart:js but my compiler still not see eval() method.
HTML code (working):

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<script>
  var a = "person.name=='James'";
  var person = {'name':'James'};
  var result = eval(a);
  console.log(result);
</script>
</body>
</html>

Dart code (not working - do not see definition of eval though import html and js lib):

import 'dart:html';
import 'dart:js';
  
void main() {
  var a = "person.name=='James'";
  var person = {'name':'James'};
  var result = eval(a);
  print(result);
}

Any idea how to import javascript method eval() into dart code?


Solution

  • Try this one:

    final result = js.context.callMethod("eval",a);
    print(result);
    

    And there is whole code:

    import 'dart:js' as js;
      
    void main() {
      var a = "person.name=='James'";
      var person = {'name':'James'};
      final result = js.context.callMethod("eval",[person,a]);
      print(result);
    }