How do you namespace a Dart class?

前端 未结 1 1337
挽巷
挽巷 2021-02-05 02:30

How do you create a namespace for a Dart class? I come from a C# background, where one would just use namespace SampleNamespace { }.

How do you achieve the

相关标签:
1条回答
  • 2021-02-05 02:46

    Dart doesn't have the concept of namespaces, but instead it has libraries. You can consider a library to be sort of equivalent to a namespace, in that a library can be made of multiple files, and contain multiple classes and functions.

    Privacy in Dart is also at the library, rather than the class level (anything prefixed with an underscore is private to that library).

    An example of defining a library (using the example of a utilities library:

    // utilities.dart
    library utilities; // being the first statement in the library file
    

    You can make other files part of the same library by using the part keyword. Part files are only used to help organize your code; you can put all your classes in a single library file, or split them among several part files (or part files and the library file) - it has no effect on the execution. It is stylistic to put the main library file in a parent folder, and part files in a src/ folder.

    Expanding the example to show Part files.

    // utilities.dart
    library utilities;
    
    part "src/string_utils.dart";
    part "src/date_utils.dart";
    

    Those part files then link back to the library they are part of by using the part of statement:

    // src/string_utils.dart
    part of utilities;
    
    // functions and classes
    String reverseString(s) => // implementation ....
    
    String _stringBuilder(strings) => // a private (to the library) function, 
                                      // indicated by the leading underscore
    
    //... snip other classes and functions
    

    Now that you have a library containing a function, you can make use of that library elsewhere by importing the library:

     // my_app.dart;
     import "path/to/library/utilities.dart";
    
     main() {
       var reversed = reverseString("Foo");
       // _stringBulider(["a","b"]); // won't work - this function is 
                                     // only visible inside the library
     }
    

    If you want to alias your library to avoid clashes (where you might import two libraries, both containing a reverseString() function, you use the as keyword:

     // my_app.dart;
     import "path/to/library/utilities.dart";
     import "some/other/utilities.dart" as your_utils;
    
     main() {
       var reversed = reverseString("Foo"); 
       var your_reversed_string = your_utils.reverseString("Bar");
     }
    

    The import statement also makes use of packages, as imported by pub, Dart's package manager, so you can also host your library on github or elsewhere, and reference your library as so:

     // my_app.dart;
     import "package:utilities/utilities.dart";
    
     main() {
       var reversed = reverseString("Foo");        
     }
    

    The pub dependency is defined in a pubspec.yaml file, which tells pub where to find the library. You can find out more at pub.dartlang.org

    It is important to note that only the library file can:

    • contain import statements. Part files cannot.
    • contain the library keyword. Part files cannot.
    • contain part files. Part files cannot.

    One final point is that a runnable app file can (and is likely to be) a library file, and can also be made of part files

     // my_app.dart;
     library my_app;
    
     import "package:utilities/utilities.dart";
    
     part "src/edit_ui.dart";
     part "src/list_ui.dart";
     part "src/foo.dart";
    
     main() {
       var reversed = reverseString("Foo");    
       showEditUi(); // perhaps defined in edit_ui.dart....?
     }
    
    0 讨论(0)
提交回复
热议问题