Comments (12)
Hello, your Posts
also need to extend Equatable
from equatable.
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.
Hello, your
Posts
also need to extendEquatable
in example the class Posts extend with Equatable but same thing
from equatable.
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.
I have the same issue when trying to emit new state after updating item in list
from equatable.
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.
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 extendEquatable
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.
wait for fix... have same problem
from equatable.
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.
You can try this
@override
bool operator ==(Object other) => identical(
posts.hashCode,
other.hashCode,
);
from equatable.
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.
Closing this since there arenβt any actionable steps I can take without a minimal reproduction sample.
from equatable.
Related Issues (20)
- Same hashcode for different new lists HOT 1
- Equality and Hashcode differ for Sets with different order
- Equatable doesn't checks for Equality for variables in parent class HOT 3
- feat request: add `stringifyProps` HOT 4
- Enum implementing Equatable related classes HOT 5
- Same object, same runtimeType but runtimeType== other.runtimeType is false HOT 3
- Storing the hashCode HOT 3
- Doesn't work with List<File> as property HOT 1
- `runtimeType` prevents from using generic type HOT 5
- Provide lint for forgotten fields in props HOT 3
- `Equatable` is redundant with `EquatableMixin` HOT 2
- Upgrade sdk to include Dart 3 HOT 3
- A Map considered equal even if its not HOT 3
- Why do we need to compare runtimeType? HOT 5
- Support for mutable class objects HOT 3
- `Foo(1) == Foo(1.0)` is false HOT 2
- Do Equatable actually need to be immutable HOT 1
- Publish a new version HOT 2
- Is that logical? Object equals but element not equals. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from equatable.