تخطَّ إلى المحتوى

Avoid Flutter Imports

هذا المحتوى غير متوفر بلغتك بعد.

newdartrecommended

Avoid introducing dependencies on Flutter within business logic components (Bloc or Cubit instances).

Layering an application is a key part of building a maintainable codebase and helps developers iterate quickly and with confidence. Each layer should have a single responsibility and be able to function and tested in isolation. This allows you to contain changes to specific layers, minimizing the impact on the entire application.

As a result, business logic components should generally manage feature state and be decoupled from the UI layer. Events should flow into business logic components from the UI layer and state should flow out of the business logic layer into the UI layer.

Keeping business logic components decoupled from Flutter provides the ability to reuse business logic across multiple platforms/frameworks (e.g. Flutter, AngularDart, Jaspr, etc.).

DO NOT import Flutter within your business logic components.

BAD:

counter_cubit.dart
import 'package:bloc/bloc.dart';
// Avoid depending on Flutter!
// Prefer to keep business logic decoupled from the UI layer.
import 'packages:flutter/material.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
}

GOOD:

counter_cubit.dart
import 'package:bloc/bloc.dart';
class CounterCubit extends Cubit<int> {
CounterCubit() : super(0);
}

To enable the avoid_flutter_imports rule, add it to your analysis_options.yaml under bloc > rules:

analysis_options.yaml
bloc:
rules:
- avoid_flutter_imports