Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent importing files from flutter framework in dart?

Tags:

flutter

dart

In a flutter project, I'm following n-tier architecture. Where in a layer I don't want to allow importing files from the flutter framework. Is there a way to prevent importing files from flutter or any other layer?

I don't want to allow writing

import 'package:flutter/material.dart';

in my data and domain layer

like image 805
Niamul Hasan Avatar asked Oct 31 '25 06:10

Niamul Hasan


1 Answers

I know this is a bit old but I don't see any answers yet, so I thought to share with you what I've found regarding the subject. There are several ways to enforce the imports in your Flutter project (I documented them in details here) :

  1. Using private and public : This enforces types to be private to a file (or a library) and we can only leave what we want others to depend on as public.
  2. Using packages and dart analyzer : This techniques leverages the use of dart packages inside flutter projects in order to encapsulate a set of types and classes together and only export what we want to export. We can then use analysis_options.yaml to enable linter rules that can be triggered when we don't respect package import and try to import things directly with relative paths.
  3. Using external packages like import_lint, dart_code_metrics, or dart_arch_test
like image 135
Abdelkrim Bournane Avatar answered Nov 02 '25 20:11

Abdelkrim Bournane



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!