How to solve "The property 'length' can't be unconditionally accessed because the receiver can be 'null'"

2,332

Solution 1

Had the same error, mine was solved when I put "AsyncSnapshot" before noteData

Solution 2

That's related to Dart null-safety. One way to resolve this is using the bang operator since you are sure that noteData.data is never null (you are checking that before using), e.g.:

noteData.data!.length, noteData.data![index]['title'] and so on.

This solution could seem cumbersome, so I would recommend creating a local variable before using noteData.data:

...
} else {
  final data = noteData.data; // noteData.data! could be needed here, not sure

  return Padding(
    padding: EdgeInsets.all(8.0),
      child: ListView.builder(
        itemCount: data.length,
        ...
      ),
    ),
  );
}
Share:
2,332
Sakshi Khatod
Author by

Sakshi Khatod

Updated on December 29, 2022

Comments

  • Sakshi Khatod
    Sakshi Khatod over 1 year

    I have added conditions too for the reciever to not be null but the error persists. These are 2 errors which I am unable to solve.Please help! I am making "Notes" application where I am storing values in sqflite db.

            body: FutureBuilder(
              future: getNotes(),
              builder: (context, noteData) {
                switch (noteData.connectionState) {
                  case ConnectionState.waiting:
                    {
                      return Center(child: CircularProgressIndicator());
                    }
                  case ConnectionState.done:
                    {
                      if (noteData.data == null) {
                        return Center(
                          child: Text("You don't have any notes yet, create one!"),
                        );
                      } else {
                        return Padding(
                          padding: EdgeInsets.all(8.0),
                          child: ListView.builder(
                             itemCount: noteData.data.length,   //Error
                            itemBuilder: (context, index) {
                              String title = noteData.data[index]['title'];// Error-The method '[]' can't be 
                                                                      //unconditionally invoked because the receiver //can be 'null'.
                              String body = noteData.data[index]['body'];
                              String creation_date =
                                  noteData.data[index]['creation_date'];
                              int id = noteData.data[index]['id'];
    
                 
    
  • Sakshi Khatod
    Sakshi Khatod almost 3 years
    It didn’t work.It showed the same error again.I used bang operator too but it caused the same issue
  • mkobuolys
    mkobuolys almost 3 years
    Then maybe the bang operator is needed after noteData as well? It would help if you post the code of getNotes() method.
  • Sakshi Khatod
    Sakshi Khatod almost 3 years
    I have added he code.Please check out @mkobuolys
  • mkobuolys
    mkobuolys almost 3 years
    Oh, I see... First of all, you should refactor your code to return List<NoteModel> and not dynamic, that would be easier to handle later.
  • Noor Hossain
    Noor Hossain about 2 years
    @mkobuolys, has some codes ?
  • tazboy
    tazboy about 2 years
    Thank you! It doesn't seem like that should be needed because when you hover over snapshot it says it's an AsyncSnapshot<dynamic> snapshot. So annoying.