Palladiusz
Palladiusz

Reputation: 23

My .map method doesnt work properly in function

I dont know why the method .map is totally ignored. Even after deleted conditions, it doesnt add widgets to my list. It seems like groupsToRename.map() doesnt exist at all.

List<Widget> listRenamedGroups(
          List<MapEntry<int, String>> groupsToRename) {
        final List<Widget> resultList = [];
        final currentGroups =
            (bloc.state as ListSuccessfullState<GroupViewModel>).viewModels;
        groupsToRename.map((groupRenameEntry) {
          final correspondingOldEntry = currentGroups
              .firstWhere((element) => element.groupId == groupRenameEntry.key);
          if (correspondingOldEntry == null ||
              correspondingOldEntry.name != groupRenameEntry.value) {
            resultList.add(Text(
              '${correspondingOldEntry.name} --> ${groupRenameEntry.value}',
              textAlign: TextAlign.center,
              style: const TextStyle(
                  fontWeight: FontWeight.w600,
                  fontSize: 20,
                  color: ColorsExtension.textGreyLight),
            ));
          }
        });
        return resultList;
      }

Upvotes: 1

Views: 132

Answers (2)

Didier Prophete
Didier Prophete

Reputation: 2031

You should really not be using .map() here since you are not assigning the result to anything. A for loop is really what you want in your case.

Now, as to why in your examples, it appeared that .map() wasn't doing anything is becasue in dart, .map() is lazy in a sense that it really returns an iterator. It's not really iterating over the list unless you explicitly consume the list (say, with .toList()).

Upvotes: 1

Autocrab
Autocrab

Reputation: 3757

Add toList() after map(), or replace map with forEach

Upvotes: 1

Related Questions