Dart: library and export for web apps

994 views Asked by At

I am experimenting with my first Dart web app and do not expect to make any part of my app a reusable library for other apps/libs. As such, I do not have a lib directory in my project; rather, I have a web directory.

I guess my intent is to have my web directory look like this:

web/
    Main.dart <-- where my main method is
    logging/
        Logger.dart
        LogLevel.dart
    model/
        Signin.dart
        Signout.dart
    view/
        SigninView.dart
        signinView.html
    presenter/
        SigninPresenter.dart
    ...lots of other packages

Several questions:

  1. Should my entire application (everything under web) be considered to be a part of the same library? If so, would I then put library myapp; at the top of every Dart file? Otherwise, what is the level of granularity for a library? Should I put it package-level, and have Logger and LogLevel inside library logging;? Is it at the class/file level and have a library logger and a library log_level?
  2. Is my web directory set up correctly? I'm coming from Java so I'm treating web the same as Java/Maven's src/main/java directory, and setting up a package structure under web that makes sense to me...
  3. I understand that the import keyword is for importing source types from other packages. But what about export - what does that do?
1

There are 1 answers

1
Shailen Tuli On

I would recommend that you have a lib/ directory and put shared code within it. If the code isn't directly to be used by other parts of your app, move that code to the src/ directory within lib/. I realize that you are not planning to make your app reusable by other apps, but code which is consumed by different parts of your app should probably live within lib/.

A lot of this is a matter of convenience and convention, but I try to keep my web/ directory pretty lean, even going so far as to place only those .dart files in it that are directly referenced in a .html file. Your mileage may vary, but I find that this convention (putting shared code in lib/ and web-specific code in web/ helps keep things clear).

As for library directives, I like to use a single namespace which is the same as the name of the app.

So, a library in lib/models.dart could be declared like this:

library myApp.models;

A library in `web/foo/bar.dart' could be declared like this:

library myApp.web.foo.bar;