How can I import all files in a folder?

前端 未结 3 2004
余生分开走
余生分开走 2021-02-07 12:44

Say I got a bunch of dart scripts in a folder,

Is there anything I can do like import \'foo/*.dart\'?

P.S. What if I got an array of filenames and

相关标签:
3条回答
  • 2021-02-07 13:24

    You need to import each library individually.

    What you can do is to create a library that imports all other libraries and reexports them.
    you can then import this one library and get all libraries imported at once.

    library all_in_one;
    
    export library1.dart;
    export library2.dart;
    export library3.dart;
    
    0 讨论(0)
  • 2021-02-07 13:25

    This would be highly unsecure, this is why it is not allowed by design. An attacker would be able to run any malicious code just adding a file with the right name to your folder.

    0 讨论(0)
  • 2021-02-07 13:38

    You could also use the "part of" library composition:

    Create 1 .dart file that is your lib eg.: lib.dart and add at the start of this file.:

    library lib
    

    For every file in your folder add a:

    part "somefile.dart"
    part "otherfile.dart"
    

    In all files that are part of this library add at the start:

    part of lib
    

    In other files and libs you can then import all those file just via:

    import "lib.dart"
    

    This will import all the parts of your library (folder). Keep in mind that the "lib.dart" file is now responsible for all the imports of your lib files. So to import something to "somefile.dart" you add the import to "lib.dart". All imports are then available in all the lib files.

    0 讨论(0)
提交回复
热议问题