Home > other >  The argument type 'Future<String> Function()' can't be assigned to the paramete
The argument type 'Future<String> Function()' can't be assigned to the paramete

Time:04-29

I'm trying to get webview height using runJavascriptReturningResult

child: Container(
         height: () async => await webViewController.runJavascriptReturningResult("document.body.scrollHeight"),
 ...
)

Always getting error The argument type 'Future<String> Function()' can't be assigned to the parameter type 'double?'. How can I get this webview height in container ?

CodePudding user response:

I think runJavascriptReturningResult function returns a String value. First check what it returns by writing it to console or debugging. If it is proper String value to cast double. Then you should parse it like:

String retVal = await webViewController.runJavascriptReturningResult("document.body.scrollHeight").then(),

double val = double.parse(retVal);

CodePudding user response:

You need to extract your method:

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;
  }

So you use it with the FutureBuilder like this:

FutureBuilder(
  future: getHeight(),
  builder: (context, snap) {
    return SizedBox(
      height: snap.data as double?,
    );
  },
)
  • Related