Home > Back-end >  The element type 'Type' can't be assigned to the map value type
The element type 'Type' can't be assigned to the map value type

Time:10-18

I'm trying to create a way to sort some items that I have.

abstract class IComposableOrderBy {
  String sortBy();
}

class SortByCreatedAt implements IComposableOrderBy {
  @override
  String sortBy() => r'{ order_by: {created_at: $sort} }';
}

class SortByEmptyFirst implements IComposableOrderBy {
  @override
  String sortBy() => r'{ products_aggregate: {count: desc} }';
}

enum OrderBy {
  asc,
  desc,
}

enum SortOptions {
  byCreatedAt,
  byEmptyFolder,
}

The problem is... When I tried to use it as a Map, I received this error The element type 'Type' can't be assigned to the map value type 'IComposableOrderBy'.

final Map<SortOptions, IComposableOrderBy> _sortOptions = {
    SortOptions.byCreatedAt: SortByCreatedAt,
    SortOptions.byEmptyFolder: SortByEmptyFirst
  };

does Dart not have support for this?

Could you help me, please?

CodePudding user response:

If you declare Map<SortOptions, IComposableOrderBy>, you must pass instance of IComposableOrderBy class, not its class Type, change it to:

final Map<SortOptions, IComposableOrderBy> _sortOptions = {
    SortOptions.byCreatedAt: SortByCreatedAt(),
    SortOptions.byEmptyFolder: SortByEmptyFirst()
  };
  • Related