Skip to content Skip to sidebar Skip to footer

[SOLVED] The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Unable to Fix this Issue


Issue

Was watching some flutter tutorials on youtube and practising making some applications using flutter. The same code works great on the tutors while he runs the code but I am facing this error i.e The method ‘[]’ can’t be unconditionally invoked because the receiver can be ‘null’.

I am attaching my code. Tried adding a null check operator also but didn’t work.The error occurs at line 55 that is title: Text(myData[index]['body'])


FutureBuilder(
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var myData = snapshot.data;
            return ListView.builder(
              itemBuilder: (context, index) => ListTile(
                title: Text(myData[index]['body']),
              ),
            );
          } else {
            return const Center(
              child: CircularProgressIndicator(
                color: Colors.black,
              ),
            );
          }
        },
      ),

Solution

With FutureBuilde


FutureBuilder(
        future: getData(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            var mydata = snapshot.data as List;
            return ListView.builder(itemBuilder: (context, index) {
              return Container(
                decoration: BoxDecoration(border: Border.all()),
                child: Text(mydata[index]['body']),
              );
            });
          } else {
            return const Center(
              child: CircularProgressIndicator(
                color: Colors.black,
              ),
            );
          }
        },
      ),

Jaka Surya
Jaka Surya Mobile Developer Flutter

Post a Comment for "[SOLVED] The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Unable to Fix this Issue"