如何避免在dart / flutter中为每个文件编写导入文件?

Rog*_*mez 4 dart flutter

我正在制作一个Flutter应用程序,并为我使用的每个自定义小部件创建了一个文件/类。然后,我将所有这些文件导入主屏幕,但是我不喜欢它的外观。特别是因为如果我想添加另一个小部件或删除一个小部件,则需要弄乱导入内容。

是否有类似C#名称空间的内容,我可以对文件夹/名称空间中的所有文件进行一次导入?

我已经尝试成功使用library / part,但是在https://www.dartlang.org/guides/libraries/create-library-packages中说我应该避免使用part / partof。那么,我们是否期望导入每个文件?

而不是:

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';
Run Code Online (Sandbox Code Playgroud)

我想拥有:

import 'package:custom_widgets';
Run Code Online (Sandbox Code Playgroud)

Mat*_*ija 7

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';
Run Code Online (Sandbox Code Playgroud)

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.

  • 你好!谢谢您的回答。所以无论如何我都必须列出文件?是否可以在每个文件中放入某些内容以将它们分组为“custom_widgets”(例如在 c# 中声明的命名空间)?理想情况下,我不想在添加新小部件时摆弄额外的文件。 (2认同)
  • 但是如何禁用 main_package_file_with_exports.dart 中未列出的所有文件的可见性? (2认同)

小智 5

VSCode 有这个扩展可以帮助创建它。

飞镖桶文件生成器