Skip to main content

banned-usage

added in: 1.3.0
warning
⚙️

Configure some usages that you want to ban.

Example: When you add some extra functionalities to built-in Flutter functions (such as logging for showDialog), you may want to ban the original Flutter function and use your own version.

caution

When trying to ban some methods in your package, it also triggers on imported from an external package code. But you can specify the name to ban (ex: 'DateTime.now' instead of 'now').

info

This rule is an improved version of ban-name and they should not be used together.

⚙️ Config example

dart_code_metrics:
...
rules:
...
- banned-usage:
entries:
- type: List
entries:
- ident: sort
description: Use "sorted" instead
- ident: reversed
description: Do not reverse a list
- ident: showDialog
description: Please use myShowDialog in this package
- ident: strangeName
description: The name is too strange
- ident: AnotherStrangeName
description: Oops
- ident: StrangeClass.someMethod
description: Please use a NonStrangeClass.someMethod instead
- ident: DateTime.now
description: Please use a clock.now instead

Example

❌ Bad:

// suppose the configuration is the one shown below

showDialog('some_arguments', 'another_argument'); // LINT
material.showDialog('some_arguments', 'another_argument'); // LINT

var strangeName = 42; // LINT

void strangeName() {} // LINT

// LINT
class AnotherStrangeName {
late var strangeName; // LINT
}

StrangeClass.someMethod(); // LINT
NonStrangeClass.someMethod();

DateTime.now(); // LINT
DateTime.now().day; // LINT
class DateTimeTest {
final currentTimestamp = DateTime.now(); // LINT
}
DateTime currentTimestamp = DateTime('01.01.1959');

final list = [1, 2, 3];
list.sort(); // LINT

✅ Good:

myShowDialog();
NonStrangeClass.someMethod();

clock.now();
clock.now().day;

class DateTimeTest {
final currentTimestamp = clock.now();
}

final list = [1, 2, 3];
final newList = list.sorted();