Home > Software design >  flutter library dart:html using eval from javascript
flutter library dart:html using eval from javascript

Time:06-23

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?

CodePudding user response:

Try this one: 

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