Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i define an empty List for initial value of RxList of Maps

I use Getx package in my project and i want to define a list like this in controller

  RxList<UserType> usersNames = [
    UserType(name: 'komail', isActive: true),
    UserType(name: 'ali', isActive: true),
  ].obs;

this is UserType class

class UserType {
  String name;
  bool isActive;

  UserType({
    required this.name,
    required this.isActive,
  });
}

usersNames list is empty in its initial value. but when i want to define this variable like below, i get this error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  RxList<UserType> usersNames = [].obs;
A value of type 'RxList<dynamic>' can't be assigned to a variable of type 'RxList<UserType>'.
Try changing the type of the variable, or casting the right-hand type to 'RxList<UserType>'

>Solution :

In this line, you’re assigning a list of type dynamic to a list which has a type UserType.

RxList<UserType> usersNames = [].obs; // Bad

What you should rather do is, use:

RxList<UserType> usersNames = <UserType>[].obs; // Good
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading