Bloc: Immutability pattern for nested data

Created on 9 Mar 2019  路  3Comments  路  Source: felangel/bloc

I'm struggling a bit in transitioning to the immutable way of working with data. It seems simple in the Github Search example, but in more complex applications the data is usually nested. Here's a simplified version of my app's model:

class Schedules {
    final List<Schedule> list;
    final int currentList;
}
class Schedule {
    final String name;
    final String id;
    final Set<Course> courses;
}
class Course {
    final String name;
    final String teacher;
    ...
}

Does this mean whenever a user changes their schedules I have to basically clone every object in the hierarchy? Like so:

final newSchedules = Schedules (
    list: list
        .map((schedule) => Schedule(
            id: schedule.id,
            name: schedule.name,
            courses: courses
                .map((course) => Course(
                    name: 'Math',
                    ...
                )
        .toList())

Is there a less-verbose way to do this? If your objects had 10+ fields the clone function would be pretty long and hard to maintain.

question waiting for response

Most helpful comment

Thanks for speedy response! Good to know that the state equality checking just whether the instances are the same. I think I'm going to have to clone the objects because I want to save a history of operations, and that won't work if the every step of history references the same schedule object.

Also, thanks for this great library! Best Dart state management I've discovered so far, and seemingly the only way to share code across Flutter/Angular.

All 3 comments

@timtraversy great question!

The way bloc works is it compares the currentState to the state you yield in mapEventToState before determining if there should be a state change/transition. By default Dart treats different instances of an object as not being equal even if their properties are the same so all you really need to do is make sure you yield a new instance of your bloc state and everything should work as expected.

There are optimizations you can make to avoid rebuilds/transitions if the states have the same properties and that's when you get into the types of problems you're mentioning. If you choose to go down that road and want to override == and hashCode then I suggest implementing convenience methods for doing the basic mutation-free manipulation operations on each model. Let me know if that helps! 馃憤

Thanks for speedy response! Good to know that the state equality checking just whether the instances are the same. I think I'm going to have to clone the objects because I want to save a history of operations, and that won't work if the every step of history references the same schedule object.

Also, thanks for this great library! Best Dart state management I've discovered so far, and seemingly the only way to share code across Flutter/Angular.

No problem! Thanks for the positive feedback and please let me know if you have any other questions 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  路  3Comments

rsnider19 picture rsnider19  路  3Comments

nhwilly picture nhwilly  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

nerder picture nerder  路  3Comments