在 Flutter 中查找

问题描述 投票:0回答:5

我正在学习如何在 Flutter 中使用查找,这是我遇到的一个例子。

void main() {
  Map<List<String>, String> lookup = {
    ['foo', 'bar']: 'foobar',
    ['baz', 'qux']: 'bazqux',
    ['hello', 'world']: 'helloworld',
  };

  List<String> key = ['foo', 'bar'];

  String? result = lookup[key];

  if (result != null) {
    print('Result: $result');
  } else {
    print('No match found for $key');
  }
}

但问题是结果是“找不到 ['foo','bar'] 的匹配项,尽管代码是正确的。结果应该返回“foobar”,但我不确定问题出在哪里以及如何解决。

flutter dart lookup
5个回答
0
投票

来自文档...默认情况下,列表只等于它们自己。 https://api.dart.dev/be/180791/dart-core/List/operator_equals.html


0
投票

问题是 Dart 如何比较列表的相等性。有关如何比较列表的更多信息,请参见此处。但这在这里的情况下不起作用。

如果你用它说谎,你会得到结果,因为它们的键是同一个对象

void main() {
    List<String> key = ['foo', 'bar'];
    Map<List<String>, String> lookup = {
      key: 'foobar',
      ['baz', 'qux']: 'bazqux',
      ['hello', 'world']: 'helloworld',
    };


    String? result = lookup[key];

  if (result != null) {
    print('Result: $result');
  } else {
    print('No match found for $key');
  }
}

0
投票

问题是您正在尝试将新列表

List<String> key = ['foo', 'bar']
['foo','bar']
的现有键进行比较,由于引用不同,它们不相等,因此您需要将两个键点的引用指向同一个变量
key 

例子:

void main() {
    List<String> key = ['foo', 'bar']; //👈 define the key on the top

    Map<List<String>, String> lookup = {
      key: 'foobar',                      // use the same key reference here
      ['baz', 'qux']: 'bazqux',
      ['hello', 'world']: 'helloworld',
    };

    String? result = lookup[key];

  if (result != null) {
    print('Result: $result');
  } else {
    print('No match found for $key');
  }
}

输出:

Result: foobar

0
投票

你可以在我使用

IterableEquality
的地方使用这个功能

import 'package:collection/collection.dart';
  String? getResult(Map<List<String>, String> map, List<String> key) {
    const eq = IterableEquality();
    try {
      final data = map.entries.singleWhere((element) => eq.equals(element.key,
          key)); // actually there are other function also available

      return data.value;
    } catch (e) {
      return null;
    }
  }

然后测试

    String? result = getResult(lookup, key);

您可以查看更多关于我如何比较 Dart 中的列表是否相等?


0
投票

如声明

key
,它生成一个新对象,所以它不会被匹配。为了克服这个问题,您可以使用自定义比较功能来解决这个问题。

void main() {
  Map<List<String>, String> lookup = {
    ['foo', 'bar']: 'foobar',
    ['baz', 'qux']: 'bazqux',
    ['hello', 'world']: 'helloworld',
  };

  List<String> key = ['foo', 'bar'];

  String? result = lookup[lookup.keys.firstWhere((e)=> listEquals(e, key), orElse: () => key)];

  if (result != null) {
    print('Result: $result');
  } else {
    print('No match found for $key');
  }
}

bool listEquals<T>(List<T>? a, List<T>? b) {//This custom function which check two listed are having same content
  if (a == null) {
    return b == null;
  }
  if (b == null || a.length != b.length) {
    return false;
  }
  if (identical(a, b)) {
    return true;
  }
  for (int index = 0; index < a.length; index += 1) {
    if (a[index] != b[index]) {
      return false;
    }
  }
  return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.