Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:bloc未从列表中删除数据_Flutter_Dart_Flutter Bloc - Fatal编程技术网

Flutter 颤振:bloc未从列表中删除数据

Flutter 颤振:bloc未从列表中删除数据,flutter,dart,flutter-bloc,Flutter,Dart,Flutter Bloc,我正在尝试使用bloc创建最喜爱的新闻列表,现在如果我想添加到最喜爱的列表中,它确实会发生,但是如果我想删除它,那么列表不会得到更新,因此它不会从UI中删除 我的集团逻辑 class FavouriteBloc extends Bloc<FavouriteEvent, List<Articles>> { FavouriteBloc() : super(null); List<Articles> articles = []; @override

我正在尝试使用
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;
    }
  }
}
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(...),
                );
              });
        },
      ),
    );
  }
}
类收藏夹屏幕扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
var height=MediaQuery.of(context).size.height;
var width=MediaQuery.of(context).size.width;
返回脚手架(
appBar:appBar(),
正文:BlocBuilder(
buildWhen:(上一个,当前){
如果(上一个.length
hi-bro)添加此库
https://pub.dev/packages/equatable 
@JsonSerializable()
类文章扩展了equalable{
来源;
字符串作者;
字符串标题;
字符串描述;
字符串url;
字符串urlToImage;
日期时间发布日期;
字符串内容;
文章({
这个消息来源,,
这位作者,
这个名字,
这个.说明,,
这个.url,
这个.urlToImage,
这个.dat,
这个.内容,,
});
@凌驾
List get props=>[name];//根据要删除列表项的字段,将“name”替换为您的字段。
factory Articles.fromJson(映射json)=>
_$ArticlesFromJson(json);
}
hi bro添加此库
https://pub.dev/packages/equatable 
@JsonSerializable()
类文章扩展了equalable{
来源;
字符串作者;
字符串标题;
字符串描述;
字符串url;
字符串urlToImage;
日期时间发布日期;
字符串内容;
文章({
这个消息来源,,
这位作者,
这个名字,
这个.说明,,
这个.url,
这个.urlToImage,
这个.dat,
这个.内容,,
});
@凌驾
List get props=>[name];//根据要删除列表项的字段,将“name”替换为您的字段。
factory Articles.fromJson(映射json)=>
_$ArticlesFromJson(json);
}

如果两个对象是同一实例,Dart将进行比较。您需要重写
=
运算符或使用类似于库的方法

您需要做的第一件事是删除
buildWhen
。现在它只会在添加项目时更新(重建),而不会在删除项目时更新(重建)。 比较
urlToImage
title
author

如果两个对象是同一实例,Dart将进行比较。您需要重写
==
运算符或使用类似于库的运算符

您需要做的第一件事是删除
buildWhen
。现在它只会在添加项目时更新(重建),而不会在删除项目时更新(重建)。 比较
urlToImage
title
author

嘿,你们能分享这个模型吗class@gowthamanC好的,我已经在问题中添加了我的模型类。你们能分享模型吗class@gowthamanC好的,我已经在问题中添加了我的模型类,好的,但它在状态更改后会重建,所以我在
buildWhen
中的条件是正确的,或者我需要做一些其他的事情。当状态更改时,重建是正常的anges。您的条件是当列表大小更改时,重新生成。从列表中添加或删除项目将触发重新生成,这是正常行为。如果不重新生成,则不会看到更改的项目。tbh您需要删除
buildWhen
。您希望每次添加或删除项目时都会触发重新生成从列表中。在这种情况下没有任何意义。现在它只会在您添加项目时重建,而不会在您删除项目时重建。我忘记了“否”,它不会重建。如果您删除项目,它不会重建。对吗?这来自
buildWhen
。删除
buildWhen
,它会很好。可以,但它会在以后重建状态更改当
正确或我需要执行其他操作时,
build中的状态也会更改。当状态更改时,重建是正常的。您的状态是当列表大小更改时,重建。从列表中添加或删除项将触发重建,这是正常行为。如果不重建,您将不会看到changed articles.tbh您需要删除
buildWhen
。您希望每次添加或从列表中删除项目时都会触发重建。在这种情况下没有任何意义。现在它将仅在添加项目时重建,而不是在删除项目时重建。sry我忘记了“否”,它不会重建,如果您删除项目,它将不会重建。对吗?这来自于
buildWhen
。删除
buildWhen
就可以了。
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);
}
        buildWhen: (previous, current) {
          if(previous.length<current.length){
            return true;
          }
          return false;

        },
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;
    }
  }
}
@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);
}
@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);
}