【问题标题】:Unhandled Exception: NoSuchMethodError: Class 'List<dynamic>' has no instance method 'retainWhere' with matching arguments未处理的异常:NoSuchMethodError:类 \'List<dynamic>\' 没有具有匹配参数的实例方法 \'retainWhere\'
【发布时间】:2022-11-21 21:59:44
【问题描述】:

我正在尝试使用“retainWhere”将列表减少为只有匹配的值,但我收到的错误是

static Future getSearchHistory() async {
    final value = await _storage.read(key: _keySearchHistory);

    return value == null ? null : json.decode(value);
  }

static Future setSearchHistory(history) async {
    var results = await getSearchHistory();

    
    print('results${results.runtimeType}');

    if (results != null) {
      var res = results.retainWhere(
          (result) =>
              result['value'].toLowerCase() ==
              history['value'].toString().toLowerCase(),
          orElse: () => null);
      if (results != null) {
        results.add(history);
        final value = json.encode(results);
        await _storage.write(key: _keySearchHistory, value: value);
      }
    } else {
      final value = json.encode(history);
      await _storage.write(key: _keySearchHistory, value: value);
    }
  }

【问题讨论】:

  • 你能包括你的代码和你的错误信息吗?请不要为此使用图像。

标签: android flutter


【解决方案1】:

函数https://api.flutter.dev/flutter/dart-core/List/retainWhere.html 不接受orElse 参数。将您的代码更改为:

static Future setSearchHistory(history) async {
    var results = await getSearchHistory();

    
    print('results${results.runtimeType}');

    if (results != null) {
      var res = results.retainWhere(
          (result) =>
              result['value'].toLowerCase() ==
              history['value'].toString().toLowerCase());
      if (results != null) {
        results.add(history);
        final value = json.encode(results);
        await _storage.write(key: _keySearchHistory, value: value);
      }
    } else {
      final value = json.encode(history);
      await _storage.write(key: _keySearchHistory, value: value);
    }
  }

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 2021-10-17
    • 2021-05-29
    • 2021-04-20
    • 2022-11-24
    • 2021-11-07
    • 2019-11-20
    • 2021-02-26
    相关资源
    最近更新 更多