prefer_if_elements_to_conditional_expressions

Group: style

Maturity: stable

Dart SDK: >= 2.2.1-dev.4.0 • (Linter v0.1.85)

Since info is static, may be stale

View all Lint Rules

Using the Linter

When building collections, it is preferable to use if elements rather than conditionals.

BAD:

Widget build(BuildContext context) {
  return Row(
    children: [
      IconButton(icon: Icon(Icons.menu)),
      Expanded(child: title),
      isAndroid ? IconButton(icon: Icon(Icons.search)) : null,
    ].where((child) => child != null).toList(),
  );
}

GOOD:

Widget build(BuildContext context) {
  return Row(
    children: [
      IconButton(icon: Icon(Icons.menu)),
      Expanded(child: title),
      if (isAndroid) IconButton(icon: Icon(Icons.search)),
    ]
  );
}