Amit Aisikowitz
Amit Aisikowitz

Reputation: 143

Flutter - The method 'then' was called on null

I have a flutter project with 2 functions:

Future<dynamic> eval(String js) =>
  controller?.evaluateJavascript(js)?.catchError((_) => '');

void _getBottomPosition() async {
    final evals = await Future.wait([
        eval("(window.innerHeight + window.scrollY) >= document.body.offsetHeight"),
    ]);
    String bottmPosition = evals[0].toString() ?? '';
    isBottomPosition = bottmPosition;
    print("bottomPosition: " + isBottomPosition);
}

I'm getting the following error:

E/flutter (14430): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'then' was called on null.
E/flutter (14430): Receiver: null
E/flutter (14430): Tried calling: then<Null>(Closure: (dynamic) => Null, onError: Closure: (dynamic, StackTrace) => Null)

Can anyone help me solve this?

Thanks.

Upvotes: 1

Views: 4930

Answers (1)

Augustin R
Augustin R

Reputation: 7799

You should try this :

Future<dynamic> eval(String js) async =>
  controller?.evaluateJavascript(js)?.catchError((_) => '');

void _getBottomPosition() async {
    final eval = await eval("(window.innerHeight + window.scrollY) >= document.body.offsetHeight");
    String bottmPosition = eval.toString() ?? '';
    isBottomPosition = bottmPosition;
    print("bottomPosition: " + isBottomPosition);
}

First add async keyword to eval method then update eval call in _getBottomPosition.

Upvotes: 1

Related Questions