Reputation: 31
I'm trying to create a list of objects - from SpaceX - where each object is a clickable link that leads to a details page, but I keep getting this error:
"type 'Future<List>' is not a subtype of type 'List' in type cast"
My Classes:
More specifically, I believe the error comes from line 22, in MyApp.dart:
futureLaunch = fetchLaunch() as Future<Launch>;
since that's the only place I cast anything as anything... but I have no idea how to fix it. Every time I change it, I only wind up with new errors.
Attempted fixes:
none of which managed to help.
Upvotes: 2
Views: 1534
Reputation: 1151
Regarding the concrete error, this is caused because fetchLaunch() returns a Future<List<Launch>>
, not a Future<Launch>
.
Why do you load the list of Launches in 2 places, main.dart and Services.dart? Right now you have a mix of using FutureBuilder and setState, this could be simplified a lot. Example (untested code, could contain errors):
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SpaceX',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FutureBuilder<List<Launch>>(
future: Services.getLaunches(),
builder: (context, snapshot) {
return Scaffold(
appBar: AppBar(
title: Text(snapshot.hasData ? 'Loading...' : 'SpaceX Launches'),
centerTitle: true,
),
body: Center(
child: Builder(builder: (context) {
if (snapshot.hasData) {
return Text(snapshot.data.first?.missionName ?? "No missions");
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return CircularProgressIndicator();
}),
),
);
},
),
);
}
}
Upvotes: 0