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

Time:01-24

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)

CodePudding user response:

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}'),
  ),

CodePudding user response:

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.

  •  Tags:  
  • Related