我正在尝试使用runJavascriptReturningResult获取webview高度
child: Container(
height: () async => await webViewController.runJavascriptReturningResult("document.body.scrollHeight"),
...
)总是得到错误的The argument type 'Future<String> Function()' can't be assigned to the parameter type 'double?'.,我如何才能在容器中获得这个网页的高度?
发布于 2022-04-28 08:45:10
我认为runJavascriptReturningResult函数返回一个字符串值。首先,通过将其写入控制台或调试来检查它返回的内容。如果是正确的字符串值,则转换为双值。那么,您应该将其解析为:
String retVal = await webViewController.runJavascriptReturningResult("document.body.scrollHeight").then(),
double val = double.parse(retVal);发布于 2022-04-28 10:14:36
您需要提取您的方法:
Future<double> getHeight() async {
String retVal = await webViewController
.runJavascriptReturningResult("document.body.scrollHeight");
// 0.0 is a fallback value in case it doesn't parse your string, you can set it whatever you want
double height = double.tryParse(retVal) ?? 0.0;
return height;
}因此,您可以像这样在FutureBuilder中使用它:
FutureBuilder(
future: getHeight(),
builder: (context, snap) {
return SizedBox(
height: snap.data as double?,
);
},
)https://stackoverflow.com/questions/72040402
复制相似问题