0

enter image description hereI have some object references of a class like this:

Patient patientOne = Patient('Person A', 'https://images.unsplash.com/photo-1545996124-0501ebae84d0?ixid=MXwxMjA3fDB8MHxzZWFyY2h8OHx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
      8, 2, 'Pending', '10-08-2015', true);
  Patient patientTwo = Patient('Person B', 'https://images.unsplash.com/photo-1544005313-94ddf0286df2?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MTF8fGh1bWFufGVufDB8fDB8&ixlib=rb-1.2.1&w=1000&q=80',
      8, 5, 'Cancel', '23-12-2019', false);
  Patient patientThree = Patient('Person C', 'https://images.unsplash.com/photo-1554151228-14d9def656e4?ixid=MXwxMjA3fDB8MHxzZWFyY2h8NHx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
      8, 7, 'Cancel', '01-02-2019', false);
  Patient patientFour = Patient('Person D', 'https://upload.wikimedia.org/wikipedia/commons/e/ec/Woman_7.jpg',
      8, 4, 'Pending', '20-09-2018', true);
  Patient patientFive = Patient('Person E', 'https://cdn.pixabay.com/photo/2017/08/07/14/15/portrait-2604283__340.jpg',
      8, 6, 'Pending', '28-04-2017', false);

I want to store those reference variables (patientOne , patientTwo ... patientFive ) into a list. How to do that ?

1 Answer 1

2
final List<Patient> patients = <Patient>[];
patients.addAll([patientOne,patientTwo,patientThree,...]);

or

final List<Patient> patients = [
Patient('Person A', 'https://images.unsplash.com/photo-1545996124-0501ebae84d0?ixid=MXwxMjA3fDB8MHxzZWFyY2h8OHx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
      8, 2, 'Pending', '10-08-2015', true),

Patient('Person A', 'https://images.unsplash.com/photo-1545996124-0501ebae84d0?ixid=MXwxMjA3fDB8MHxzZWFyY2h8OHx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
      8, 2, 'Pending', '10-08-2015', true)
,...
] ;
Sign up to request clarification or add additional context in comments.

4 Comments

I was trying the first one and at "patients" its showing like "The name of a constructor must match the name of the enclosing class." @David
can you add screenshot of how did you used it?
You should use it like that only in case if you instantiate patients into theirs instances before. So in your case my usage will be after your code when patiens are instantiated
Ok, @sushreesomamohanty after your screenshot, you should use my second approach, bcs you cant invoke addAll method in context of class, you could use that only in constructor or method for example. Use second approach and you should be just fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.