Monday, July 20, 2026
HomeiOS Developmentandroid - Cannot obtain updates from ValueNotifier when used from MethodCallHandler

android – Cannot obtain updates from ValueNotifier when used from MethodCallHandler

[ad_1]

I’ve a easy Widget with a ValueListenableBuilder that listens to a ValueNotifier.

The construct operate of the ValueListenablebuilder is rarely triggered when updating the worth of the ValueNotifier from a local technique name (utilizing setMethodCallHandler).

As a substitute, if I take advantage of valueNotifier.pay attention(() {}), I can obtain new values even within the construct operate. The native code, in reality, emits a brand new String every second.

That is the minimal code to breed the difficulty:

class Important extends StatelessWidget {

  Important({Key key}) : tremendous(key: key);

  @override
  Widget construct(BuildContext context) {

    platform.invokeMethod("startNativeMethod");
    platform.setMethodCallHandler(handleNativeMethod);

    Future.delayed(Length(seconds: 3)).then((worth) {
      // this WORKS and updates the Textual content!
      resourceNotifier.worth = "check";
    });

    resourceNotifier.addListener(() {
      // this ALWAYS works (prints "check" after which native updates every second)
      print(resourceNotifier.worth);
    });

    return ValueListenableBuilder(
        valueListenable: resourceNotifier,
        builder: (context, String worth, baby) {
          // that is triggered solely with "check"
          // (when up to date from Future.delayed)
          return Container(baby: Textual content(worth ?? "Empty")); 
        });
  }


  ValueNotifier<String> resourceNotifier = ValueNotifier(null);
  MethodChannel platform = const MethodChannel("com.instance.instance");

  Future<dynamic> handleNativeMethod(MethodCall name) async {
    // this NEVER updates the Textual content() widget. At all times triggers the listener as a substitute.
    resourceNotifier.worth = "good day from native";
  }
}

This goes additionally past ValueNotifiers. I’ve additionally tried transferring to a Stateful Widget and setting state from handleNativeMethod. Even setState does not work from there.

Thanks prematurely for the assistance!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments