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

Time:01-19

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.

  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>'

CodePudding user response:

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
  •  Tags:  
  • Related