flutter - Flutter:块不从列表中删除数据
问题描述
我正在尝试使用创建最喜欢的新闻列表bloc
,现在如果我想添加到最喜欢的列表中,它确实会发生,但如果我想删除它,那么列表不会更新,所以它不会从 UI 中删除。
我的集团逻辑,
class FavouriteBloc extends Bloc<FavouriteEvent, List<Articles>> {
FavouriteBloc() : super(null);
List<Articles> articles = [];
@override
Stream<List<Articles>> mapEventToState(FavouriteEvent event) async* {
switch (event.eventType) {
case EventType.add:
articles.add(event.articles);
yield articles;
break;
case EventType.delete:
articles.remove(event.articles);
yield articles;
break;
}
}
}
活动课,
enum EventType {add, delete}
class FavouriteEvent{
Articles articles;
EventType eventType;
FavouriteEvent.add({this.articles,this.eventType});
FavouriteEvent.remove({this.articles,this.eventType});
}
用户界面部分,
在此屏幕中,当我添加到收藏夹时,它会显示我添加的卡片列表,然后我用onTap
它来将其从列表中删除,但这并没有发生
class FavouriteScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(),
body: BlocBuilder<FavouriteBloc, List<Articles>>(
buildWhen: (previous, current) {
if(previous.length<current.length){
return true;
}
return false;
},
builder: (context, newsList) {
if (newsList == null) {
return Center(
child: Text(
week7.Strings.noFav,
style: Theme.of(context).textTheme.headline6,
),
);
}
return ListView.builder(
itemCount: newsList.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
BlocProvider.of<FavouriteBloc>(context).add( //<--- this is how I'm trying to remove
FavouriteEvent.remove(
articles: Articles(
urlToImage: newsList[index].urlToImage,
title: newsList[index].title,
author: newsList[index].author
),
eventType: EventType.delete));
},
child: Card(...),
);
});
},
),
);
}
}
模型类,
@JsonSerializable()
class Articles {
Source source;
String author;
String title;
String description;
String url;
String urlToImage;
DateTime publishedAt;
String content;
Articles({
this.source,
this.author,
this.title,
this.description,
this.url,
this.urlToImage,
this.publishedAt,
this.content,
});
factory Articles.fromJson(Map<String, dynamic> json) =>
_$ArticlesFromJson(json);
}
所以谁能告诉我我在这里做错了什么?
解决方案
hi bro add this lib
https://pub.dev/packages/equatable
@JsonSerializable()
class Articles extends Equatable{
Source source;
String author;
String title;
String description;
String url;
String urlToImage;
DateTime publishedAt;
String content;
Articles({
this.source,
this.author,
this.title,
this.description,
this.url,
this.urlToImage,
this.publishedAt,
this.content,
});
@override
List<Object> get props => [name];// depending on which field you want to remove the list item, replace "name" with your field.
factory Articles.fromJson(Map<String, dynamic> json) =>
_$ArticlesFromJson(json);
}
推荐阅读
- javascript - uBlock 阻止“返回顶部”按钮 (JavaScript)
- pandas - Pandas 使用字典将空替换为基于列的值
- node.js - 在架构中引用 objectId 和其他参数的最佳方法是什么?
- php - 科学数字的格式
- sqlite - 从递归查询的结果创建表(with 语句)
- python - 如何在 Python 的 Pandas 中用我想要的东西替换特定列的子字符串?
- python - 如何在 Seaborn 图上画一个圆?
- xpath - XPATH:选择其子节点不包含某些文本的节点
- r - rstudio 中出现意外的“=”
- ios - 当AKPlayer到达指定时间时如何得到通知