Logan Blackisle
Logan Blackisle

Reputation: 31

Error type 'Future<List<Object>>' is not a subtype of type 'List<Object>' in type cast

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:

Error screenshot

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

Answers (2)

Josh
Josh

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

Rached Khalledi
Rached Khalledi

Reputation: 187

you have to await the list because it is a future list

Upvotes: 1

Related Questions