英文:
InAppWebView getContentHeight always returns 0 on Android
问题
我使用 InAppWebView 在我们的应用中显示网页内容。
为了确保它仅占用必要的空间,我监听 onLoadStop 回调,然后设置 SizedBox 的高度。
在这里,我尝试获取内容的高度:
final height = await controller.getContentHeight();
但它总是返回 0。
在 iOS 上,我总是能够获得正确的高度。
英文:
I use InAppWebView to display a web content in our app.
To ensure that it only takes the necessary space I listen to the onLoadStop callback to then set a SizedBoxs height.
There I try to get the content height:
final height = await controller.getContentHeight();
But it always returns 0.
On iOS I always get the right height.
答案1
得分: 0
问题在于Android过早调用了Android WebView的onPageFinished回调。答案在这里找到。
添加以下代码解决了这个问题:
if (Platform.isAndroid) {
   await Future.delayed(
      const Duration(milliseconds: 1000),
   );
}
这样可以解决问题,并且
final height = await controller.getContentHeight();
返回了正确的高度。
英文:
The problem is that Android calls the onPageFinished callback for the Android Webview too early. Answer found here.
Adding
if (Platform.isAndroid) {
   await Future.delayed(
      const Duration(milliseconds: 1000),
   );
}
solves the problem and
final height = await controller.getContentHeight();
returns the correct height.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论