Flutter: passing data to WebView

Viewed 5237

I get the location in my Flutter app. I have a WebView and I want to pass the location inside it (to handle it after in the site opened within the webview).

var userLocation = Provider.of<UserLocation>(context);

And my WebView:

WebView(
      initialUrl: widget.url,
      javascriptMode: JavascriptMode.unrestricted,
 )

Is there a way to do it? My goal is like a binding between flutter and angular sending continuous location data.

2 Answers

normally To receive data from flutter we add js function in html

function fromFlutter(location) {
    document.getElementById("locationId").innerHTML = location;
    sendBack();
}

and then in flutter create a web view controller then

_controller.evaluateJavascript('fromFlutter("From Flutter")');

or for variable containing location

_controller.evaluateJavascript('fromFlutter("$locationVar")');

I have this same problem, and the original question mentioned Angular which is what I am also using. So let me share my solution.

You can pass route params to an angular page, so if you have the data going from your flutter app to a database, say Firebase, then you can pass the specific document ID as a route param in flutter webview, and any data you are passing in flutter will auto sync up to the route that you have called in angular.

Related