unnecessary-flutter-imports

Path
lint/correctness/unnecessary-flutter-imports
Default
On (recommended)
Origin
Falcon
Domain
flutter
Rule
455 of 512
recommendedflutter

Disallow unused Flutter framework and dart:async imports.

Usage is based on resolved symbol identity and import combinators. Unknown unprefixed names conservatively keep an unrestricted import, avoiding guesses from capitalization or substrings.

Invalid

example.dartdart
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart' as flutter show Text;

void main() {
  print('Hello World');
}

class MyApp {
  void doSomething() {
    print('No Flutter symbols used');
  }
}
Remove this unnecessary Flutter import.
1import 'package:flutter/material.dart';
Remove this unnecessary Flutter import.
1import 'package:flutter/material.dart';
2import 'package:flutter/foundation.dart';
Remove this unnecessary Flutter import.
2import 'package:flutter/foundation.dart';
3import 'dart:async';
Remove this unnecessary Flutter import.
3import 'dart:async';
4import 'package:flutter/services.dart';
Remove this unnecessary Flutter import.
4import 'package:flutter/services.dart';
5import 'package:flutter/widgets.dart';
Remove this unnecessary Flutter import.
5import 'package:flutter/widgets.dart';
6import 'package:flutter/material.dart' as flutter show Text;

Valid

example.dartdart
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'dart:async';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      home: Scaffold(
        appBar: AppBar(title: const Text('Home')),
        body: const Center(
          child: Text('Hello'),
        ),
      ),
    );
  }
}

Future<void> asyncExample() async {
  await Future.delayed(const Duration(seconds: 1));
  debugPrint('Done');
}

// Uses Stream from dart:async
Stream<int> createStream() {
  return Stream.fromIterable([1, 2, 3]);
}

// Uses widgets
Widget createButton() {
  return ElevatedButton(
    onPressed: () {},
    child: const Text('Click me'),
  );
}

// Uses Color from Flutter
void styleContainer() {
  final container = Container(
    color: Color.fromARGB(255, 100, 150, 200),
  );
}

Configure unnecessary-flutter-imports

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "unnecessary-flutter-imports": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/unnecessary-flutter-imports: <reason>
  • unnecessary-nullablecheck-unnecessary-nullablecross-file

    Flags private function or method parameters whose nullable type is unnecessary.

  • unnecessary-nullable-return-typeunnecessary_nullable_return_type

    Disallow nullable return types on functions that never return null.

  • unnecessary-statementsunnecessary_statementsrecommended

    Flags expression statements whose expression has no clear effect.

  • unrelated-type-equality-checksunrelated_type_equality_checksrecommended

    Reports ==/!= comparisons and relational ==/!= patterns whose operand types are unrelated.

  • unused-codecheck-unused-codecross-file

    Report public top-level declarations that nothing references.

  • unused-dependenciescross-file

    Report regular pubspec dependencies with no Dart import or export in the analyzed package.

  • unused-filescheck-unused-filescross-file

    Report lib/ files that no project entrypoint can reach.

  • use-build-context-synchronouslyuse_build_context_synchronouslyrecommendedflutter

    Flags BuildContext use after an asynchronous gap without a mounted guard.