Skip to content

NoSuchMethodError: Class 'bool' has no instance method 'then' in MethodChannel handlers (Dart 3 compatibility) #14

Description

@aligh98

Hello,

When using the latest version of the Adivery Flutter plugin (4.8.8) on Dart 3, an unhandled exception is thrown in the Flutter framework whenever a MethodChannel event (like onAdLoaded, onAdLoadFailed, etc.) is received from the native side.

This happens because the handle(MethodCall call) methods in both BannerAdEventHandler and NativeAdEventHandler return a boolean true while their return type is declared as Future<dynamic>, but the method itself is not marked as async.

Stacktrace

NoSuchMethodError: Class 'bool' has no instance method 'then'.
Receiver: true
Tried calling: then<dynamic>(Closure: (dynamic) => dynamic)
    at BannerAdEventHandler.handle (package:adivery/adivery_ads.dart:159)
    at MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:608)

Root Cause

In lib/adivery_ads.dart:

  // Missing 'async' keyword here
  Future<dynamic> handle(MethodCall call) { 
    switch (call.method) {
      // ...
    }
    return true as dynamic; // Returns a boolean, but MethodChannel expects a Future
  }

Flutter's MethodChannel._handleAsMethodCall tries to call .then() on the returned result. Since true is a bool and not a Future, it throws a NoSuchMethodError which breaks the error handling and app state.

Solution

Add the async keyword to the handle methods in both BannerAdEventHandler and NativeAdEventHandler, or return Future.value(true).

Example Fix:

  Future<dynamic> handle(MethodCall call) async {
    switch (call.method) {
        // ...
    }
    return true;
  }

Please fix this in the next release, as it currently causes apps to crash or silently fail when an ad fails to load. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions