Error to recover a Future List<Class> Dart

Asked

Viewed 733 times

-1

People are having trouble recovering data from a future function when arriving in snapshot... presents this error:

AsyncSnapshot<List<Episodio>>(ConnectionState.done, null, Exception: type 'List<dynamic>' is not a subtype of type 'FutureOr<List.....

Exception: type 'List<dynamic>' is not a subtype of type 'FutureOr<List<class>>'
Onde posso está errando?

my function Future that will be passed further on FutureBuilder.

   Future <List<Episodio>> getAnime()async{
    try{
    List teste=[];
    List test=
    (widget.animes.temporada.map((t)=>t.episodios.map((e)=>Episodio(

       ))).toList()) ;

   return test;
 }catch(e){
    throw Exception(e);

  }
}

.. in the Future Builder

@override
  Widget build(BuildContext context) {

    return Scaffold(
      body: FutureBuilder(
          future: getAnime(),
          builder: (context,snapshot){
          if(!snapshot.hasData){return Center(child: CircularProgressIndicator(),);}
          else if(snapshot.hasError){return Center(child: Text("Erro"),);}
          else{
            return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (context, index){

                print (" teste:${snapshot.data[index]}");
              return ListTile(
                leading: Image.network(snapshot.data[index].toString(),fit: BoxFit.cover,width: 100,height: 50,),

              );
            });
          }
      }),
  • What is being informed are the types of data that are incompatible. Only by the snippet of code that posted could not reproduce the error. It would be better to have the full example to be able to open in Vscode without import problems.

  • A tip that I can pass on is to use your editor’s messages to see the type of return of your function and if it is compatible with what is being expected in the call you made using it.

1 answer

0

I couldn’t test it either, but maybe if you put a await along those lines:

   return await test;

will probably work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.