visitFunctionExpression method

  1. @override
Map? visitFunctionExpression(
  1. FunctionExpression node
)
override

Implementation

@override
Map? visitFunctionExpression(FunctionExpression node) {
  var functionType = node.staticType as FunctionType?;
  String? tagString;
  String? returnTypeString;
  String? returnTypeTypeArguments;
  if (functionType != null) {
    var returnType = functionType.returnType;

    tagString = functionType.getDisplayString(withNullability: true);
    returnTypeString =
        functionType.returnType.getDisplayString(withNullability: true);
    // 特殊处理一下 Widget
    //
    // itemBuilder: (context, index) {
    //   return Container();
    // }
    //
    // 会生成这样的, "Container Function(BuildContext, int)"
    //
    // 但是实际上需要是 "Widget Function(BuildContext, int)"
    //
    // Widget 是符合大部分的场景
    //
    // 应该尽量避免使用返回类型是显式类型(比如返回类型必须是Container),如果真的需要,请在回调执行的地方再做强转。
    // 或者在自定义的 DynamicWidgetBuilder 中做特殊处理
    //
    // class SugarCommon {
    //   SugarCommon._();
    //   static Container Function() returnContainer(Widget Function() input) {
    //     Container Function() builder =(){
    //       return input() as Container;
    //     };
    //     return builder;
    //   }
    // }
    //
    // class CustomDynamicWidgetBuilder extends DynamicWidgetBuilder {
    //   CustomDynamicWidgetBuilder(
    //     super.proxyMirror,
    //     super.page,
    //     super.bound, {
    //     super.bundle,
    //   });
    //   @override
    //   dynamic convert(BuildContext context, Map map, Map? methodMap,
    //       {Domain? domain}) {
    //     var name = map[tag];
    //     if(name =='SugarCommon.returnContainer') {
    //        dynamic fairFunction = pa0(map);
    //        assert(fairFunction is Map);
    //        Container Function() builder = (
    //        ) {
    //          return convert(
    //            context,
    //            FunctionDomain.getBody(fairFunction),
    //            methodMap,
    //          ) as Container;
    //        };
    //        return builder;
    //     }
    //     return super.convert(context, map, methodMap, domain:domain);
    //   }
    // }
    if (returnType is InterfaceType) {
      if (returnType.allSupertypes
          .any((element) => element.element.name == 'Widget')) {
        var nullabilityString = returnTypeString.endsWith('?') ? '?' : '';
        tagString = tagString.replaceFirst(
            returnTypeString, 'Widget' + nullabilityString);
        returnTypeString = 'Widget' + nullabilityString;
      }
      if (returnType.typeArguments.isNotEmpty) {
        returnTypeTypeArguments = returnType.typeArguments
            .map((e) => e.getDisplayString(withNullability: true))
            .join(',');
      }
    }
  }

  return _buildFunctionExpression(
    _visitNode(node.parameters),
    _visitNode(node.body),
    isAsync: node.body.isAsynchronous,
    tag: tagString,
    returnType: returnTypeString,
    returnTypeTypeArguments: returnTypeTypeArguments,
  );
}