Home > Back-end >  The argument type 'UserListModal?' can't be assigned to the parameter type 'User
The argument type 'UserListModal?' can't be assigned to the parameter type 'User

Time:01-18

get error in return userList![index]

List<UserListModal?>? userList = [];
                  itemCount: userList!.length,
                  itemBuilder: (_, index) {
                    return UserListView(user:userList![index]);
                  }))),```

CodePudding user response:

Done it's front and back both two sides null safety

ListView.builder(
              itemCount: userList!.length,
              itemBuilder: (_, index) {
                return  UserListView(user:userList![index]!);
              }))),

CodePudding user response:

In your business logic, is it possible that userList can have null elements? Like:- userList = [null, null, UserListModal, null,]

If not, then please change the declaration to this:-

final List<UserListModal> userList = []

Explanation: Since you are assigning an empty list this this variable, userList cannot be null. So you can change the List<UserListModal?>? to List<UserListModal?>. And if you are not adding any null elements to this list, you can remove the remaining "?" i.e. change List<UserListModal?> to List.

I think you should read up on null safety in detail.

And using "final" makes sure that userModal cannot be re-assigned. This is a best practice for operating with lists.

  • Related