I have a WebView where I parse the Site-content after the user presses a Button. If loading the site for the first time (initUrl), I get all the correct data. However if changing the sites within the browser, the site content is not loaded correctly. It feels like it only loads the first site correctly.
For example:
This is my initial Url:
'https://www.saturn.de/de/product/_apple-airpods-pro-in-ear-kopfhorer-bluetooth-weiss-2771284.html'
I parse it and I get all the correct images. However if going to another site and then back, I get other data. Why is this happening? I am lost here... What am I missing?
This is my WebView:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Web View Example'),
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: WebView(
initialUrl:
'https://www.saturn.de/de/product/_apple-airpods-pro-in-ear-kopfhorer-bluetooth-weiss-2771284.html',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
},
javascriptChannels: <JavascriptChannel>{
_extractDataJSChannel(context),
},
onPageStarted: (String url) {
setState(() {
_isLoading = true;
});
},
onPageFinished: (String url) {
setState(() {
_imagesWithSize = [];
_currentUrl = url;
_isLoading = false;
});
},
),
),
Row(
children: [
IconButton(
onPressed: () {
_controller.goBack();
},
icon: Icon(
Icons.arrow_back,
size: 50,
),
),
Flexible(
child: RoundedCornersTextButton(
title: 'GET',
isEnabled: !_isLoading,
onTap: () async {
await _getData();
},
),
),
IconButton(
onPressed: () {
_controller.goForward();
},
icon: Icon(
Icons.arrow_forward,
size: 50,
),
),
],
),
SizedBox(
height: 20,
),
RoundedCornersTextButton(
title: 'Google',
isEnabled: !_isLoading,
onTap: () async {
await _controller.loadUrl('https://www.google.com');
},
),
],
),
),
);
}
JavascriptChannel _extractDataJSChannel(BuildContext context) {
return JavascriptChannel(
name: 'Flutter',
onMessageReceived: (JavascriptMessage message) {
String pageBody = message.message;
},
);
}
As you can see I tried something with JavaScript but I can't get it to work.. Any help is appreciated!