Flutter:将数据传递给 WebView

Edo*_*lla 6 location webview flutter angular

我在我的 Flutter 应用程序中获得了位置。我有一个 WebView,我想传递它里面的位置(在 webview 中打开的站点中处理它)。

var userLocation = Provider.of<UserLocation>(context);
Run Code Online (Sandbox Code Playgroud)

还有我的 WebView:

WebView(
      initialUrl: widget.url,
      javascriptMode: JavascriptMode.unrestricted,
 )
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?我的目标就像颤振和角度发送连续位置数据之间的绑定。

小智 1

通常为了从flutter接收数据,我们在html中添加js函数

function fromFlutter(location) {
    document.getElementById("locationId").innerHTML = location;
    sendBack();
}
Run Code Online (Sandbox Code Playgroud)

然后在 flutter 中创建一个 web 视图控制器

_controller.evaluateJavascript('fromFlutter("From Flutter")');
Run Code Online (Sandbox Code Playgroud)

或者对于包含位置的变量

_controller.evaluateJavascript('fromFlutter("$locationVar")');
Run Code Online (Sandbox Code Playgroud)