buildHashCodeSnippet function

String buildHashCodeSnippet(
  1. List<({bool isCollection, bool isNamed, bool isNullable, String name, String type})> fields, {
  2. DartFileEditBuilder? builder,
})

Builds hashCode getter snippet.

Implementation

String buildHashCodeSnippet(
  List<
    ({
      String name,
      String type,
      bool isNamed,
      bool isNullable,
      bool isCollection,
    })
  >
  fields, {
  DartFileEditBuilder? builder,
}) {
  if (builder != null && fields.any((f) => f.isCollection)) {
    builder.importLibrary(Uri.parse('package:collection/collection.dart'));
  }

  final hashList = fields
      .map((f) {
        if (f.isCollection) {
          return 'const DeepCollectionEquality().hash(${f.name})';
        }
        return f.name;
      })
      .join(', ');

  return '''

  @override
  int get hashCode => Object.hash($hashList);''';
}