I am making a flutter application and I created a file/class for each custom widget I am using. Then, I imported all of these files into the main screen but I don't like how it looks. Especially because if I want to add another widget or delete the one I would need to fiddle with the imports.
Is there something like C# namespaces where I can just make one import for all files in folder/namespace?
I already tried using library/part with success but then in https://www.dartlang.org/guides/libraries/create-library-packages says that I should avoid using part/part of. So, are we expected to import each and every file?
Instead of having:
import 'package:custom_widgets/custom_multiplechoice.dart'; import 'package:custom_widgets/custom_singlechoice.dart'; import 'package:custom_widgets/custom_time.dart'; import 'package:custom_widgets/custom_yesnochoice.dart'; import 'package:custom_widgets/custom_date.dart';
I would like to have:
import 'package:custom_widgets';
A barrel is a way to rollup of imports from several files into a single convenient file.
The minimal requirements for a library are: pubspec file. The pubspec. yaml file for a library is the same as for an application package—there is no special designation to indicate that the package is a library.
Yes there is, you can use export
to achieve what you want.
You can place all your widgets in a folder, for example libs/src/
and then create file custom_widgets.dart
in libs/
and use export
like this inside custom_widgets.dart
:
export 'src/custom_multiplechoice.dart'; export 'src/custom_singlechoice.dart'; export 'src/custom_time.dart'; export 'src/custom_widgets/src/custom_yesnochoice.dart'; export 'src/custom_date.dart';
Once you import
custom_widgets.dart
, all those widgets will be available to you.
Check this out, its all explained here: Dart: Organizing a library package
Update:
In Dart there are no namespaces
like in most other languages. Dart uses libraries
for encapsulation, data hiding. Only way to import a class into your code is to use import in the beginning of your file, which should also be a library.
I have an issue with this too. Imagine a situation where you want to import library dynamically. Let's say you would like to implement MVC pattern
in your application, if you are doing this on the server, you would have a Router class
that would analyze URL and decide what Controller class
to instantiate and what Method
from that Controller to invoke. Now every URL would trigger different Controller, and you don't know it in advance, its up to your Router to detect a Class to instantiate. What you need to do in this situation is import every Controller that could get instantiated at the beginning of your file. And I have issues with that. What if your application gets big and you have to import lets say 20 Controller classes, just so Router / Dispatcher can invoke one of them, and in reality you will invoke only one Controller, as there is only Controller per URL.
Don't have issues with manual loading of Libraries, if they are going to be used, but for situation like described above, Dart fails as there is no "auto loading" of classes like in for example PHP, where you can use auto loaders that use namespaces to find out about location of your class and instantiate a class in the middle of the code dynamically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With