简体   繁体   中英

How could i pass a List as an Argument or Parameter in Dart (Flutter)?

Created a List of Users in Dart but don't know how to pass a List as an Argument in Dart. Can anyone help me

List<User> user = [
      User(username: 'Muhammad Wajahat', desc: 'Lorem ipsum dolor sit amet consectaur.'),
      User(username: 'Muhammad Ali', desc: 'Lorem ipsum dolor sit amet consectaur.'),
      User(username: 'Masab Mehmood', desc: 'Lorem ipsum dolor sit amet consectaur.'),
      User(username: 'Faiq Tanveer', desc: 'Lorem ipsum dolor sit amet consectaur.')
];

I've tried this but this doesn't work:

card(user)
void someFunction(List<User> users) {
  // do something
}

And calling the function:

someFunction(users) ;

--

EDIT: Okay, it is specifically about a Card.

Well, depends... if you want to somehow put all user information on one card... But I assume you want to create a bunch of cards, one for each user?

There is a number of ways to reach that. This is one of the easiest:

for (var user in users)
  Card(
    child: Text('Name: ${user.name}'),
  ),

Type Inference let you send any type of data in functin Create method like this:

void card(var users) {
  // Do whatever you want to do with list 
}

and you can call this card method where you want like this:

card(user)

and you will be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM