2

我有一个像这样的递归 SplayTreeMap (自动生成)(伪代码):

SplayTreeMap map = <SplayTreeMap>{
  Entry('path', 'cooltype'): <SplayTreeMap>{
    Entry('subpath', 'othercooltype'): <SplayTreeMap>{
      Entry('subsubpath', 'coolcooltype'): <SplayTreeMap>{},
    },
    Entry('othersubpath', 'othercooltype'): <SplayTreeMap>{},
  },
}

类条目如下所示:

class Entry implements Comparable<Entry> {
  String path;
  String type = 'defaulttype';
  int songs = 0;

  Entry(this.path, this.type);

  @override
  int compareTo(Entry other) =>
      this.path.toLowerCase().compareTo(other.path.toLowerCase());

  @override
  String toString() => 'Entry( ${this.path} )';

  String get name => path.split('/').lastWhere((e) => e != '');
}

我想要做的是将 1 添加到Entry('subpath', 'othercooltype').songs. 我试过map.update了,但没有成功([ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '(dynamic) => dynamic' is not a subtype of type '(SplayTreeMap<dynamic, dynamic>) => SplayTreeMap<dynamic, dynamic>' of 'update')。我还尝试保存它的值,删除密钥并添加更新的密钥,但它有问题(有时有效,有时无效)。

我当前的代码:

    Entry entry = Entry(relativeString, type);
    if (type == 'othercooltype') entry.songs++;
    if (!submap.containsKey(entry)) {
      submap[entry] = SplayTreeMap();
      setState(() => valueChanged(value++));
    } else {
      // update key with songs++
    }
4

1 回答 1

1

您可以访问密钥,只需更改它的歌曲属性而不更改地图。像这样的东西:

Entry keyEntry = submap.keys.firstWhere((key) => key == entry);
keyEntry.songs = 1;

并且您将需要覆盖 Entry 相等运算符

@override
bool operator ==(other) {
  return this.path == other.path;
}

@override
int get hashCode => super.hashCode;

运算符等于

于 2019-10-10T05:37:05.987 回答