Flutter Bloc equatable 状态未使用 Map of Strings 属性进行更新

Dus*_*ilk 6 dart flutter bloc flutter-bloc

我的 Bloc 状态没有更新,我发现问题可能是Map<String, Map<String, String>未正确比较的属性。如果我错了,请纠正我,但状态会在其他属性更改时更新,而不是在imageUrls属性更新时更新。

这些是我的状态对象

abstract class PropertiesState extends Equatable {
  const PropertiesState();
}

class PropertiesLoaded extends PropertiesState {
  final int count;
  final List<Property> properties;
  final Map<String, Map<String, String>> imageUrls;

  const PropertiesLoaded({
    this.count,
    this.properties,
    this.imageUrls,
  });

  @override
  List<Object> get props => [count, properties, imageUrls];
}
Run Code Online (Sandbox Code Playgroud)

imageUrls 字段可以有任何字符串键/值对。我无法找到有关如何执行此操作的任何信息。

谢谢您的帮助!

Liu*_*han 7

因为 Equatable 正在尝试比较 List 或 Map 对象的引用,因为它们不是像 int 这样的基本类型。您可以尝试深度复制 Map 对象(使用 Map.from())并替换旧的,然后使用哈希码进行比较。


Esl*_*raf 6

来自文档

Equatable属性应始终被复制而不是被修改。如果Equatable类包含ListMap作为属性,请务必分别使用List.fromMap.from以确保根据属性值而不是引用来评估相等性。

emit(PropertiesLoaded(count, properties,
Map<String, Map<String, String>>.from(imageUrls)));
Run Code Online (Sandbox Code Playgroud)