Home > Mobile >  How to return/yield from map function
How to return/yield from map function

Time:09-27

Why can I do:

  var userArray = ['User 1', 'User 2', 'User 3'];
  var userList = userArray.map((user) => user).toList();

But in a function body nor return or yield works:

  var userArray = ['User 1', 'User 2', 'User 3'];
  var userList = userArray.map((user) => {return user}).toList();
  var userList = userArray.map((user) => {yield user}).toList();

CodePudding user response:

  var userArray = ['User 1', 'User 2', 'User 3'];
  var userList = userArray.map((user) => {return user}).toList();

If you want to use {} braces, you can't use an arrow function (=>). Instead:

  var userArray = ['User 1', 'User 2', 'User 3'];
  var userList = userArray.map((user) {
    return user;
  }).toList();

(you also need the semicolon).

  •  Tags:  
  • dart
  • Related