@@ -50,7 +50,7 @@ class ${constructor.redirectedName}${data.genericsDefinitionTemplate} $_concrete
5050$_properties
5151
5252${copyWith ?.copyWithGetter (needsCast : false ) ?? '' }
53- ${methods (data , globalData , properties : constructor .properties , name : constructor .redirectedName , escapedName : constructor .escapedName , source : Source .syntheticClass )}
53+ ${methods (data , globalData , properties : constructor .properties , name : constructor .redirectedName , escapedName : constructor .escapedName , source : Source .syntheticClass , isConst : constructor . isConst )}
5454}
5555
5656${copyWith ?.interface ?? '' }
@@ -325,12 +325,13 @@ String methods(
325325 required String name,
326326 required String escapedName,
327327 required Source source,
328+ required bool isConst,
328329}) {
329330 return '''
330331${toJson (data , name : name , source : source )}
331332${debugFillProperties (data , globalData , properties , escapedClassName : escapedName )}
332333${operatorEqualMethod (data , properties , className : name , source : source )}
333- ${hashCodeMethod (data , properties , source : source )}
334+ ${hashCodeMethod (data , properties , source : source , isConst : isConst )}
334335${toStringMethod (data , globalData , escapedClassName : escapedName , properties : properties )}
335336''' ;
336337}
@@ -467,6 +468,7 @@ String hashCodeMethod(
467468 Class data,
468469 List <Property > properties, {
469470 required Source source,
471+ required bool isConst,
470472}) {
471473 if (! data.options.equal) return '' ;
472474
@@ -491,25 +493,26 @@ String hashCodeMethod(
491493 property.name,
492494 ];
493495
494- if (hashedProperties.length == 1 ) {
495- return '''
496- $jsonKey
497- @override
498- int get hashCode => ${hashedProperties .first }.hashCode;
499- ''' ;
500- }
501- if (hashedProperties.length >= 20 ) {
496+ final hashCodeExpression = hashedProperties.length == 1
497+ ? '${hashedProperties .first }.hashCode'
498+ : hashedProperties.length >= 20
499+ ? 'Object.hashAll([${hashedProperties .join (',' )}])'
500+ : 'Object.hash(${hashedProperties .join (',' )})' ;
501+
502+ if (isConst || source == Source .mixin ) {
502503 return '''
503504$jsonKey
504505@override
505- int get hashCode => Object.hashAll([${ hashedProperties . join ( ',' )}]) ;
506+ int get hashCode => $ hashCodeExpression ;
506507''' ;
507508 }
508509
509510 return '''
510511$jsonKey
512+ int? _cachedHashCode;
513+
511514@override
512- int get hashCode => Object.hash(${ hashedProperties . join ( ',' )}) ;
515+ int get hashCode => _cachedHashCode ??= $ hashCodeExpression ;
513516''' ;
514517}
515518
0 commit comments