GithubHelp home page GithubHelp logo

Comments (12)

yshean avatar yshean commented on June 9, 2024 2

Hello, your Posts also need to extend Equatable

from equatable.

JosephNK avatar JosephNK commented on June 9, 2024 2
class PostState extends Equatable {
  final List<Posts> posts;
  final int timestamp;

  const PostState({
    this.posts = const [],
    this.timestamp = 0,
  });

  PostState copyWith({List<Posts>? posts, int? timestamp}) {
    PostState(posts: posts ?? this.posts, timestamp: timestamp ?? 0);
  }

  @override
  List<Object?> get props => [posts, timestamp];
}

@MajdSallora The same bug occurred, so I added a timestamp property to handle the hash value changing.
@felangel I hope that bug will be fixed.

from equatable.

MajdSallora avatar MajdSallora commented on June 9, 2024 1

Hello, your Posts also need to extend Equatable

in example the class Posts extend with Equatable but same thing

from equatable.

RobertPietraru avatar RobertPietraru commented on June 9, 2024 1

So because you are working with a list of posts, it gets a little trickier. The way Bloc works is that it compares the old state with the new state to figure out whether it needs to build again. By default, dart checks for instances to see if 2 classes are equal, hence why it works if you remove Equatable. (You create a new instance when you use copyWith and then you emit)

List<Object?> get props => posts;

Now, another issue is that you're modifying the posts directly. You should never modify any field in the state, instead you should create a copy of the state and replace it. For example, if you want to add a new post do the following:

// working
  void addPost(Post post) {
    /// create a copy of the current list
    final list = state.posts.toList();

    /// modify it however you want
    list.add(post);

    /// add it to the new state
    emit(state.copyWith(posts: list));

    /// this works because you are basically replacing the old list with a
    /// completely new one, not modifying the old list
  }

  // not working
  void addPostTheWrongWay(Post post) {
    /// use the reference to the list
    final list = state.posts;

    /// modify it however you want
    list.add(post);

    /// add it to the new state
    emit(state.copyWith(posts: list));

    /// this doesn't work because you are modifying the old list.
    /// When Bloc then checks for changes, it will see that the old list and the
    /// "new" list (it's actually the old list) are equal
  }

The state file:


class PostState extends Equatable {
  final List<Post> posts;

  const PostState({
    this.posts = const [],
  });

  PostState copyWith({List<Post>? posts}) {
    return PostState(
      posts: posts ?? this.posts,
    );
  }

  @override
  /// posts is made up of elements that extend Equatable
  List<Object?> get props => [posts];
}

class Post extends Equatable {
  final String id;
  final String name;
  final bool isLiked;
  const Post({
    required this.id,
    required this.name,
    required this.isLiked,
  });
  @override
  List<Object?> get props => [id, name, isLiked];
}

If you want to like a post:

void likePost(int index) {
    final posts = state.posts.toList();

    /// this is when something like copyWith comes in handy
    posts[index] = Post(
      id: posts[index].id,
      name: posts[index].name,
      isLiked: true,
    );
    emit(state.copyWith(posts: posts));
  }

By the way, toList creates a copy of the list.

from equatable.

AbdullahSh-25 avatar AbdullahSh-25 commented on June 9, 2024

I have the same issue when trying to emit new state after updating item in list

from equatable.

felangel avatar felangel commented on June 9, 2024

Hi @MajdSallora πŸ‘‹
Can you please provide a link to a minimal reproduction sample? As @yshean pointed out, a class which extends Equatable should also have all its properties also extend Equatable in order to equality comparisons to work properly.

from equatable.

MajdSallora avatar MajdSallora commented on June 9, 2024

Hi @MajdSallora πŸ‘‹ Can you please provide a link to a minimal reproduction sample? As @yshean pointed out, a class which extends Equatable should also have all its properties also extend Equatable in order to equality comparisons to work properly.

I make class Posts extands with equatable but same thing when emit in bloc
if do like this its work

    emit(state.copyWith(posts:[]) );
    emit(state.copyWith(posts:list) );

but it will build twice!

from equatable.

morfair avatar morfair commented on June 9, 2024

@felangel

wait for fix... have same problem

from equatable.

felangel avatar felangel commented on June 9, 2024

I'm not able to help without a minimal reproduction sample. Can anyone who is still experiencing this issue please provide a minimal reproduction sample that clearly illustrates the issue? Thanks!

from equatable.

Kaival-Patel avatar Kaival-Patel commented on June 9, 2024

You can try this

@override
  bool operator ==(Object other) => identical(
        posts.hashCode,
        other.hashCode,
      );

from equatable.

nandanurseptama avatar nandanurseptama commented on June 9, 2024

I usually do this, when i need to update List in an instance of state.

var lastPostsState = state.posts;

emit(state.copyWith(posts : [...lastPostsState],);

from equatable.

felangel avatar felangel commented on June 9, 2024

Closing this since there aren’t any actionable steps I can take without a minimal reproduction sample.

from equatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.