When to use part/part of versus import/export in Dart?

前端 未结 3 1445
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 04:04

I do not completely understand the difference between part/part of and import/export when using libraries in Dart. For e

3条回答
  •  無奈伤痛
    2020-12-05 04:26

    Let's suppose we have a Dart library called mylib, whose file is lib/mylib.dart.

    library mylib;
    
    // Definitions
    

    That library can be included in the main.dart file as

    import 'package:mypackage/mylib.dart';
    

    When you create a new library and use other libraries you want to make available automatically when using your package, then you use export:

    library mylib;
    
    export 'otherlib.dart';
    
    // Definitions
    

    You can use the show keyword to import/export only some parts of a library (like a class or something).


    You are using the part of directive wrong here. You can't use both library and part of, which is used to specify the contents that belong to a library. For example, you can split your library file in more than one file (the parts):

    Suppose we have in the file mylib.dart:

    library mylib;
    
    part 'src/class1.part';
    // More parts
    

    And then we have in another file src/class1.part the part specified in mylib.dart

    part of mylib;
    
    class Class1 { 
      /* ... */
    }
    

提交回复
热议问题