Also getting
Try making the call condtional (using ?.) or adding a null check to the target
for the same line(s):
Text("${snapshot.data[index]}")
If I do that I get only the error message from the title.
I have actually copy/pasted a FutureBuilder from another project of mine where it is working perfectly fine but in this one I get this error. The only difference is that in the not working project I see this:
AsyncSnapshot<Object?>
and in the working project I see this:
AsyncSnapshot<dynamic>
If I hover over snapshot
The entire section looks like this:
FutureBuilder(
future: http_functions.myAsyncFunction(),
builder: (context, snapshot) {
return snapshot.hasData
? Scrollbar(
controller: _scrollController,
isAlwaysShown: true,
thickness: 4,
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
shrinkWrap: true,
controller: _scrollController,
itemCount: 10,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text("${snapshot.data[index]}"),
),
);
},
),
)
: const CircularProgressIndicator();
},
)
While myAsyncFunction()
returns a List<dynamic>
using return json.decode(response.body);
You need to define the type parameter of the FutureBuilder. And cast the snapshot.data to not null by using the !
operator
FutureBuilder<List<dynamic>>(
future: http_functions.myAsyncFunction(),
builder: (context, snapshot) {
return snapshot.hasData
? Scrollbar(
controller: _scrollController,
isAlwaysShown: true,
thickness: 4,
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
shrinkWrap: true,
controller: _scrollController,
itemCount: 10,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text("${snapshot.data![index]}"),
),
);
},
),
)
: const CircularProgressIndicator();
},
)