A value of type 'Object?' can't be assigned to a variable of type 'List'.
and error in List data = snapshot.data;
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: FutureBuilder(
future: spaceProvider.getRecomendedSpace(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Space> data = snapshot.data;
return Column(
children:
data.map((item) => SpaceCard(item)).toList());
}
return Center(
child: CircularProgressIndicator(),
);
},
),
),
CodePudding user response:
add type to theFutureBuilder
widget, like this:
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: FutureBuilder<List<Space>>( // here
future: spaceProvider.getRecomendedSpace(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Space> data = snapshot.data;
return Column(
children:
data.map((item) => SpaceCard(item)).toList());
}
return Center(
child: CircularProgressIndicator(),
);
},
),
),
note that the function spaceProvider.getRecomendedSpace()
must return a List<Space>
CodePudding user response:
When calling a future
inside FutureBuilder whatever we return
from the future
function moves to snapshot
and by default type of snapshot is object
so whenever you want to receive data in same type that you are returning
, you have to tell snapshot
it's type
. by using AsyncSnapshot
.
AsyncSnapshot<Type>
See Example below
Padding(
padding: const EdgeInsets.symmetric(horizontal: 24),
child: FutureBuilder(
future: spaceProvider.getRecomendedSpace(),
builder: (context,AsyncSnapshot<List<Space>> snapshot) {
if (snapshot.hasData) {
List<Space> data = snapshot.data;
return Column(
children:
data.map((item) => SpaceCard(item)).toList());
}
return Center(
child: CircularProgressIndicator(),
);
},
),
),