How to reference another file in Dart?

后端 未结 4 1181
温柔的废话
温柔的废话 2020-11-28 07:04

I know you can use the library, import and even #import, but which is correct?

I have got two files, MainClass.dart

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 08:04

    Although i am answering very late, but the answer may help new developer.

    Always use pubspec.yaml file in your dart package(application/library).

    once you run pub get command it will add your local library in the dependencies list in .packages file.

    Consider i have following project structure.

    To refer to the content of greeting.dart in my main.dart file i should add the library as below

    import 'package:my_project_name/greeting.dart'
    

    Once imported we can use the content of greeting.dart file in our main.dart file. Note: we have not used the actual path as you can see 'lib' directory is missing.

提交回复
热议问题