Ajay_Kumar
Ajay_Kumar

Reputation: 1387

type Future<List<Item>> is not subtype of List error in flutter

My function defination is below

Future<List<Item>> fetchGitUsers() async {
final response = await http.get('https://');

if (response.statusCode == 200) {
  GitUsers gitUser =  GitUsers.fromJson(json.decode(response.body));
  return gitUser.items;
 } 

} / Function end

class GitUsers {
    List<Item> items;
 }


class ... extends State<SearchController> {

@override
  void initState() {
    super.initState();
    gitUsers = fetchGitUsers() as List<Item>;
  }
}

But I am getting below error on emulator screen..

type Future<List<Item>> is not subtype of List in type cast.

Upvotes: 0

Views: 365

Answers (1)

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12673

You didn't add await

Try this

void getUsers() async{
   gitUsers = await fetchGitUsers();
}

@override
  void initState() {
    super.initState();
    getUsers();
  }
}

If you want to use the git users in a UI (e.g ListView), consider using FutureBuilder.

Like this

FutureBuilder(
  future: fetchGitUsers(),
  builder: (context, snapshot){
    if(!snapshot.hasData) return CircularProgressIndicator();

    return ListView();
  }
)

Upvotes: 1

Related Questions