diff --git a/engine/src/flutter/lib/ui/dart_runtime_hooks.cc b/engine/src/flutter/lib/ui/dart_runtime_hooks.cc index be7a88ddaf..5145f11107 100644 --- a/engine/src/flutter/lib/ui/dart_runtime_hooks.cc +++ b/engine/src/flutter/lib/ui/dart_runtime_hooks.cc @@ -318,14 +318,16 @@ static std::string GetFunctionName(Dart_Handle func) { } void GetCallbackHandle(Dart_NativeArguments args) { - const char* kAnonymousClosureName = ""; Dart_Handle func = Dart_GetNativeArgument(args, 0); std::string name = GetFunctionName(func); std::string class_name = GetFunctionClassName(func); std::string library_path = GetFunctionLibraryUrl(func); - // TODO(24394): check !Dart_IsTearOff(func) instead of string comparison. - if (name.empty() || (name == kAnonymousClosureName)) { + // `name` is empty if `func` can't be used as a callback. This is the case + // when `func` is not a function object or is not a static function. Anonymous + // closures (e.g. `(int a, int b) => a + b;`) also cannot be used as + // callbacks, so `func` must be a tear-off of a named static function. + if (!Dart_IsTearOff(func) || name.empty()) { Dart_SetReturnValue(args, Dart_Null()); return; } diff --git a/engine/src/flutter/testing/dart/plugin_utilities_test.dart b/engine/src/flutter/testing/dart/plugin_utilities_test.dart index 9d8849f40f..27d7d60d57 100644 --- a/engine/src/flutter/testing/dart/plugin_utilities_test.dart +++ b/engine/src/flutter/testing/dart/plugin_utilities_test.dart @@ -27,7 +27,7 @@ void main() { expect(topClosure, isNotNull); expect(topClosure(), "top"); - // Static method callback + // Static method callback. final hGetInt = PluginUtilities.getCallbackHandle(Foo.getInt); expect(hGetInt, isNotNull); expect(hGetInt, isNot(0)); @@ -39,5 +39,9 @@ void main() { // Instance method callbacks cannot be looked up. final foo = new Foo(); expect(PluginUtilities.getCallbackHandle(foo.getDouble), isNull); + + // Anonymous closures cannot be looked up. + final anon = (int a, int b) => a + b; + expect(PluginUtilities.getCallbackHandle(anon), isNull); }); }