0
Hello, I am trying to bring a single result of each consuming title from an API. As this picture shows I’m having repeated titles
What way could I make it right, it follows code as far as I’ve tried with distinct
class AnimeBloc
class AnimeBloc{
final BehaviorSubject<List<Animes>> _listController =BehaviorSubject<List<Animes>>.seeded([]);
Sink<List<Animes>> get listIn => _listController.sink;
Observable<List<Animes>> outList;
Api api= Api();
AnimeBloc(){
outList=_listController.stream.distinct().asyncMap((d)=>api.getAnimeName());
print("teste $outList");
}
dispose(){
_listController.close();
}
}
Here api class
Dio dio = Dio();
Future<List<Animes>> getAnimes()async{
Response response =await dio.get(requestAnimes);
if(response.statusCode==200){
List<Animes> animes=
(response.data as List).map((item)=> Animes.fromJson(item)).toList();
return animes;
}else {
Exception("Erro na requisição");
}
}
and my screen with StreamBuilder
StreamBuilder(
stream: bloc.outList,
builder: (context, AsyncSnapshot<List<Animes>> snapshot){
if(!snapshot.hasData){return Center(child: CircularProgressIndicator(),);}
else if(snapshot.hasError){return Center(child: Text("Error "),);}
List<Animes> animes = snapshot.data;
return ListView.builder(
itemCount: animes.length,
itemBuilder:(context, index){
return
GestureDetector(
child: AnimeMainTile(
snapshot.data[index]
),
onTap: (){
print( snapshot.data[index]);
},
);
}
);
},
),
),
I don’t remember if the
Set
maintains the insertion order (which could be something important in displaying this app, it wasn’t clear to me), but I don’t think– Jefferson Quesado
Ah, you also need to implement
equals
and thehashCode
in classAnime
to ensure the functioning ofSet
– Jefferson Quesado
Exactly, it is necessary to implement Equal and hash, it is based on a hash that sets store objects. O
Set
does not guarantee you order, it means that if the order is the same great otherwise then you will have to order.– Bruno Miguens