Too Many Import Statements? Try Barrel File

Photo by Charl Folscher on Unsplash

Too Many Import Statements? Try Barrel File

OluwaMayowa's photo
ยทApr 20, 2022ยท

1 min read

Play this article


If you are used to watching cowboy movies ๐Ÿค , then barrels shouldn't be new to you first off you see the men with nice-looking hats then ranches with beautiful horses and most importantly barrels filled with liquors. But am not talking movies, today am talking barrels as it relates to software development.

Dev Vs Import Statements

How often do you write โ˜๐Ÿฟ while coding ๐Ÿค”? you probably have lost count, I mean there is always something to import from somewhere. but sometimes these Import statements can be so much that it gets confusing.

Barrel File to the rescue


imagine a file where you export all your classes and resources to and all you need do is import that single file whenever you need to access any class or data.
In the case of dart/flutter one can create a dart file say widgets.dart, export all classes and resources to that file and all you need to do is import that single dart file and you have access to whatever you need, like in the code snippet below.

export 'user_profile_image.dart';
export 'upcoming_rooms.dart';
export 'rooms_card.dart';
export 'room_user_profile.dart';
export 'package:flutter_clubhouse_ui/config/palette.dart';

I find this very resourceful as it keeps my code-space clean and organised.

Adios for now ๐Ÿ˜Š...

Share this