avoid-non-null-assertion

Same as avoid-non-null-assertion in Dart Code Metrics

Group
Style
Path
lint/style/avoid-non-null-assertion
Default
Off, opt in
Origin
Dart Code Metrics
Rule
106 of 512

Flags the null-assertion operator ! except on a resolved Map index.

Invalid

example.dartdart
// Test cases for avoid-non-null-assertion rule
// All violations are annotated inline

void testNullAssertion() {
  String? nullable = "test";
  final x = nullable!;
  print(x);
}

void printUserName(User? user) {
  print(user!.name);
  print(user!.email);
}

int getValue(int? value) {
  return value! + 10;
}

List<String> getList(List<String>? items) {
  return items!;
}

class Widget {
  String? _title;

  String getTitle() {
    return _title!;
  }

  void render() {
    final context = _context!;
    context.build();
  }
}

Map<String, dynamic> parseResponse(Map<String, dynamic>? data) {
  // The outer index `!` (`[...]!`) is exempt; only `data!` is flagged.
  final nested = data!['key'];
  return nested;
}

Future<String> asyncOperation(Future<String>? future) {
  return future!;
}

void multipleAssertions() {
  final a = value1!;
  final b = value2!;
  final c = (nested!.property)!;
}

// Regression: the violation must still be found inside Dart 3 containers
// (pattern declaration, labeled statement, switch expression and subject,
// collection if/spread, record field).
void containersRegression(int rcount, Object? maybe) {
  final (ra, _) = (maybe!, 0);
  lbl: {
    final rb = maybe!;
    print(rb);
  }
  final rc = switch (rcount) {
    0 => maybe!,
    _ => null,
  };
  final rd = switch (maybe!) {
    _ => 0,
  };
  final re = [if (rcount > 0) maybe!];
  final rf = [...[maybe!]];
  final rg = (p: maybe!, q: 0);
  print([ra, rc, rd, re, rf, rg]);
}
Avoid using the null assertion operator `!`.
5 String? nullable = "test";
6 final x = nullable!;
Avoid using the null assertion operator `!`.
10void printUserName(User? user) {
11 print(user!.name);
Avoid using the null assertion operator `!`.
11 print(user!.name);
12 print(user!.email);
Avoid using the null assertion operator `!`.
15int getValue(int? value) {
16 return value! + 10;
Avoid using the null assertion operator `!`.
19List<String> getList(List<String>? items) {
20 return items!;
Avoid using the null assertion operator `!`.
26 String getTitle() {
27 return _title!;
Avoid using the null assertion operator `!`.
30 void render() {
31 final context = _context!;
Avoid using the null assertion operator `!`.
37 // The outer index `!` (`[...]!`) is exempt; only `data!` is flagged.
38 final nested = data!['key'];
Avoid using the null assertion operator `!`.
42Future<String> asyncOperation(Future<String>? future) {
43 return future!;
Avoid using the null assertion operator `!`.
46void multipleAssertions() {
47 final a = value1!;
Avoid using the null assertion operator `!`.
47 final a = value1!;
48 final b = value2!;
Avoid using the null assertion operator `!`.
48 final b = value2!;
49 final c = (nested!.property)!;
Avoid using the null assertion operator `!`.
48 final b = value2!;
49 final c = (nested!.property)!;
Avoid using the null assertion operator `!`.
55void containersRegression(int rcount, Object? maybe) {
56 final (ra, _) = (maybe!, 0);
Avoid using the null assertion operator `!`.
57 lbl: {
58 final rb = maybe!;
Avoid using the null assertion operator `!`.
61 final rc = switch (rcount) {
62 0 => maybe!,
Avoid using the null assertion operator `!`.
64 };
65 final rd = switch (maybe!) {
Avoid using the null assertion operator `!`.
67 };
68 final re = [if (rcount > 0) maybe!];
Avoid using the null assertion operator `!`.
68 final re = [if (rcount > 0) maybe!];
69 final rf = [...[maybe!]];
Avoid using the null assertion operator `!`.
69 final rf = [...[maybe!]];
70 final rg = (p: maybe!, q: 0);

Valid

example.dartdart
// Good cases for avoid-non-null-assertion rule
// No violations expected

void testNullCoalescing() {
  String? nullable = "test";
  final x = nullable ?? "default";
  print(x);
}

void printUserName(User? user) {
  if (user != null) {
    print(user.name);
    print(user.email);
  }
}

int getValue(int? value) {
  return (value ?? 0) + 10;
}

List<String> getList(List<String>? items) {
  return items ?? [];
}

class Widget {
  String? _title;

  String getTitle() {
    return _title ?? '';
  }

  void render() {
    final context = _context;
    if (context != null) {
      context.build();
    }
  }
}

Map<String, dynamic> parseResponse(Map<String, dynamic>? data) {
  if (data == null) return {};
  final nested = data['key'];
  return nested is Map<String, dynamic> ? nested : {};
}

Future<String> asyncOperation(Future<String>? future) {
  return future ?? Future.value('');
}

void multipleNullChecks() {
  final a = value1 ?? 'default1';
  final b = value2 ?? 'default2';
  if (nested != null && nested.property != null) {
    final c = nested.property;
  }
}

void guardClauses(String? maybeValue) {
  if (maybeValue == null) return;
  print(maybeValue);
}

// A null-assertion on a (map) index expression is exempt.
int readCount(Map<String, int> counts, List<int> xs) {
  final a = counts['key']!;
  final b = xs[0]!;
  return a + b;
}

class Optional<T> {
  final T? _value;

  Optional(this._value);

  T getOrElse(T defaultValue) => _value ?? defaultValue;

  void ifPresent(void Function(T) callback) {
    if (_value != null) callback(_value as T);
  }
}

Configure avoid-non-null-assertion

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "avoid-non-null-assertion": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/avoid-non-null-assertion: <reason>