Home > Software design >  forEach -> return true; // error The return type 'bool' isn't a 'void',
forEach -> return true; // error The return type 'bool' isn't a 'void',

Time:06-14

Hello I try to make null safety migration, but I have an error with a forEach loop who return return true. I don't know how to write correctly in null stafety. Thank you

  String sanitize(
          String input, List<String> possibleStart, List<String> possibleEnd) {
        final String start = possibleStart.join("|");
        final String end = possibleEnd.join("|");

        final RegExp exp = RegExp("(?<=$start)(.*?)(?=$end)");
        final Iterable<Match> matches = exp.allMatches(input);



        matches.forEach((match) {
          input =
              input.replaceFirst(match.group(0)!, match.group(0)!.replaceAll(",", "§").replaceAll(":", "ø").replaceAll("/", "å"));

          return true;  // error The return type 'bool' isn't a 'void', as required by the closure's context dart flutter
        });
        return input;

      }

CodePudding user response:

function forEach isn't supposed to return anyting, you can see it from iterable.dart

void forEach(void action(E element)) {
    for (E element in this) action(element);
}
  • Related