Flutter:集团不从列表中删除数据
Flutter: bloc not removing data from the list
我正在尝试使用 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;
}
}
}
事件class,
enum EventType {add, delete}
class FavouriteEvent{
Articles articles;
EventType eventType;
FavouriteEvent.add({this.articles,this.eventType});
FavouriteEvent.remove({this.articles,this.eventType});
}
UI部分,
在这个屏幕上,当我添加到收藏夹时,它会显示我已添加的卡片列表,然后我使用 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(...),
);
});
},
),
);
}
}
型号class,
@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);
}
Dart 比较两个对象是否是同一个实例。您需要覆盖 ==
运算符或使用像 equatable.
这样的库
您需要做的第一件事是删除 buildWhen
。现在它只会在您添加项目时更新(重建),而不是在您删除它们时更新(重建)。
buildWhen: (previous, current) {
if(previous.length<current.length){
return true;
}
return false;
},
使用状态class表示状态,因为列表总是一样的,不会重建。之后调整您的小部件代码以使用 state.articles
.
class FavouriteState {
final List<Articles> articles;
FavouriteState(this.artticles);
}
class FavouriteBloc extends Bloc<FavouriteEvent, FavouriteState> {
FavouriteBloc() : super(null);
List<Articles> _articles = [];
@override
Stream<FavouriteState> mapEventToState(FavouriteEvent event) async* {
switch (event.eventType) {
case EventType.add:
_articles.add(event.articles);
yield FavouriteState(_articles);
break;
case EventType.delete:
_articles.remove(event.articles);
yield FavouriteState(_articles);
break;
}
}
}
比较 urlToImage
、title
和 author
的示例
@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,
});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Articles && runtimeType == other.runtimeType && urlToImage == other.urlToImage &&
title == other.title && author == other.author;
@override
int get hashCode => urlToImage.hashCode ^ title.hashCode ^ author.hashCode;
factory Articles.fromJson(Map<String, dynamic> json) =>
_$ArticlesFromJson(json);
}
使用 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 => [author, title, description, url, urlToImage, content];
factory Articles.fromJson(Map<String, dynamic> json) =>
_$ArticlesFromJson(json);
}
我正在尝试使用 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;
}
}
}
事件class,
enum EventType {add, delete}
class FavouriteEvent{
Articles articles;
EventType eventType;
FavouriteEvent.add({this.articles,this.eventType});
FavouriteEvent.remove({this.articles,this.eventType});
}
UI部分,
在这个屏幕上,当我添加到收藏夹时,它会显示我已添加的卡片列表,然后我使用 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(...),
);
});
},
),
);
}
}
型号class,
@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);
}
Dart 比较两个对象是否是同一个实例。您需要覆盖 ==
运算符或使用像 equatable.
您需要做的第一件事是删除 buildWhen
。现在它只会在您添加项目时更新(重建),而不是在您删除它们时更新(重建)。
buildWhen: (previous, current) {
if(previous.length<current.length){
return true;
}
return false;
},
使用状态class表示状态,因为列表总是一样的,不会重建。之后调整您的小部件代码以使用 state.articles
.
class FavouriteState {
final List<Articles> articles;
FavouriteState(this.artticles);
}
class FavouriteBloc extends Bloc<FavouriteEvent, FavouriteState> {
FavouriteBloc() : super(null);
List<Articles> _articles = [];
@override
Stream<FavouriteState> mapEventToState(FavouriteEvent event) async* {
switch (event.eventType) {
case EventType.add:
_articles.add(event.articles);
yield FavouriteState(_articles);
break;
case EventType.delete:
_articles.remove(event.articles);
yield FavouriteState(_articles);
break;
}
}
}
比较 urlToImage
、title
和 author
的示例
@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,
});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Articles && runtimeType == other.runtimeType && urlToImage == other.urlToImage &&
title == other.title && author == other.author;
@override
int get hashCode => urlToImage.hashCode ^ title.hashCode ^ author.hashCode;
factory Articles.fromJson(Map<String, dynamic> json) =>
_$ArticlesFromJson(json);
}
使用 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 => [author, title, description, url, urlToImage, content];
factory Articles.fromJson(Map<String, dynamic> json) =>
_$ArticlesFromJson(json);
}