Bloc: New state not fired

Created on 17 Jun 2019  路  3Comments  路  Source: felangel/bloc

The "BeaconsSuccess" state is a list of objects

abstract class BeaconsState extends Equatable {
  BeaconsState([List props = const []]) : super(props);
}

class BeaconsInitial extends BeaconsState {
  @override
  String toString() => 'BeaconsInitial';
}

class BeaconsSuccess extends BeaconsState {
  final List<Beacon> beacons;

  BeaconsSuccess([this.beacons = const []]) : super([beacons]);
}

class BeaconsFailure extends BeaconsState {
  final String error;
  BeaconsFailure(this.error) : super([error]);
}

The model is implementing == and hash

class Beacon {
  final String identifier;
  BeaconState state = BeaconState.unknown;

  Beacon(this.identifier, {this.state = BeaconState.unknown});

  Beacon.fromJson(Map<String, dynamic> json) : identifier = json['identifier'];

  Map<String, dynamic> toJson() => {
        'identifier': identifier,
      };

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      super == other &&
          other is Beacon &&
          runtimeType == other.runtimeType &&
          identifier == other.identifier &&
          state == other.state;

  @override
  int get hashCode => super.hashCode ^ identifier.hashCode ^ state.hashCode;

  @override
  String toString() {
    return 'Beacon{identifier: $identifier, state: $state}';
  }
}

If the model is only updating the state property then the news state is not fired

What is the conditions to fire or not the new state based on the state value ?

  • condition is perform on the hash ? equals ?
question

Most helpful comment

Tx it helps me.
Checking the == method in my bloc explains me that I was keeping a reference on a "old" object :(

pb fixed !

Tx

All 3 comments

The first thing that comes to mind for me is to still have your Beacon class extend equatable, and to set up your constructor like so:

class Beacon extends Equatable {
  final String identifier;
  BeaconState state = BeaconState.unknown;

  Beacon(this.identifier, {this.state = BeaconState.unknown}) : super([identifier]);

  Beacon.fromJson(Map<String, dynamic> json) {
    return Beacon(json['identifier']);
  }

That way you can still use your factory constructor and lean on equatable to handle all the comparison logic.

Hi @fvisticot 馃憢
Thanks for opening an issue!

As @craiglabenz mentioned, it's usually most convenient to have your models extend Equatable but I would add that you should also make sure that the BeaconState is being passed to the super call as well.

All bloc does is check if currentState == yieldedState. If that resolves to true, then the state change is ignored.

Hope that helps! 馃憤

Tx it helps me.
Checking the == method in my bloc explains me that I was keeping a reference on a "old" object :(

pb fixed !

Tx

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ricktotec picture ricktotec  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

nhwilly picture nhwilly  路  3Comments

rsnider19 picture rsnider19  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments