Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Latest commit

 

History

History
65 lines (48 loc) · 1.58 KB

avoid-collection-methods-with-unrelated-types.mdx

File metadata and controls

65 lines (48 loc) · 1.58 KB

import RuleDetails from '@site/src/components/RuleDetails';

Avoid using collection methods with unrelated types, such as accessing a map of integers using a string key.

:::info

This lint has been requested for a long time: Follow this link to see the details.

Related: Dart's built-in list_remove_unrelated_type and iterable_contains_unrelated_type.

:::

Example {#example}

❌ Bad:

final map = Map<int, String>();
map["str"] = "value"; // LINT
var a = map["str"]; // LINT
map.containsKey("str"); // LINT
map.containsValue(42); // LINT
map.remove("str"); // LINT

Iterable<int>.empty().contains("str"); // LINT

List<int>().remove("str"); // LINT

final set = {10, 20, 30};
set.contains("str"); // LINT
set.containsAll(Iterable<String>.empty()); // LINT
set.difference(<String>{}); // LINT
primitiveSet.intersection(<String>{}); // LINT
set.lookup("str"); // LINT
primitiveList.remove("str"); // LINT
set.removeAll(Iterable<String>.empty()); // LINT
set.retainAll(Iterable<String>.empty()); // LINT

✅ Good:

final map = Map<int, String>();
map[42] = "value";
var a = map[42];
map.containsKey(42);
map.containsValue("value");
map.remove(42);

Iterable<int>.empty().contains(42);

List<int>().remove(42);

final set = {10, 20, 30};
set.contains(42);
set.containsAll(Iterable<int>.empty());
set.difference(<int>{});
primitiveSet.intersection(<int>{});
set.lookup(42);
primitiveList.remove(42);
set.removeAll(Iterable<int>.empty());
set.retainAll(Iterable<int>.empty());