skip to Main Content

get error in return userList![index]

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

2

Answers


  1. Chosen as BEST ANSWER

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

    My SDK Version is Flutter 3.7.0-1.4

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

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search