skip to Main Content

I am creating a Map with:

Map<int,List<OverlapParams>> map = new Map<int,List<OverlapParams>>();

And I want to upsert elements to the Map, so like, when Map key 2 is not existing, I want to save a List<OverlapParams> there. – if the Map key is exisiting, I want to add the item overlapitem to the List in the map.

Here is my code:

  final overlapitem = OverlapParams(childParentData.hashCode.toString(), DateTimeRange(
    start: childParentData.startDateTime ?? DateTime(2000),
    end: childParentData.endDateTime ?? DateTime(2000),
  ), {});
  int pos = childParentData.position ?? 0;

  map.update(
    pos,
    (existingValue) => (existingValue as List<OverlapParams>).add(overlapitem),
    ifAbsent: () => [overlapitem],
  );

but I get folowing error with underlined (existingValue as List<OverlapParams>).add(overlapitem):

error: The return type 'void' isn't a 'List<OverlapParams>', as required by the closure's context. 

I dont understand what void means in this context?

2

Answers


  1. You need to return a value in the map.update function.
    List.add(value) does not return anything

    try this

    map.update(
    pos,
    (existingValue)  {
      (existingValue as List<OverlapParams>).add(overlapitem);
      return existingValue;
    },
    ifAbsent: () => [overlapitem],
    );
    

    I haven’t tested this out, but if existing value is immutable, then try this

    map.update(
    pos,
    (existingValue)  {
      var oldList = (existingValue as List<OverlapParams>);
      oldList.add(overlapitem);
      return oldList;
    },
    ifAbsent: () => [overlapitem],
    );
    
    Login or Signup to reply.
  2. The second parameter of the update method is V update(V value).

    What does that mean in English? A method that receives a parameter of type V and returns an instance of type V.

    Now lets look at what you passed as parameter:

    (existingValue) => (existingValue as List<OverlapParams>).add(overlapitem),
    

    Now, it clearly is a method. It clearly get a parameter. What does it return? Nothing on the first glance. On second look, since you have no explicit return, it returns the result of the expression (existingValue as List<OverlapParams>).add(overlapitem). The result is void, since that is what add returns.

    So that is why your compiler is complaining: your function is not returning what it is supposed to return.

    You can explicitly return something:

    (existingValue) => { 
      (existingValue as List<OverlapParams>).add(overlapitem); 
      return existingValue; 
    }
    

    If for some reason you want a new list to replace the old one (that is what the update method is meant for), you could return a new list from your anonymous function:

    (existingValue) => [overlapitem,...existingValue],
    

    I have removed the as part, this should not be neccessary, since your V type is already defined to be List<OverlapParams>.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search