importdart

dart import and part of directives in same file


I'm writing a dart file:

import 'something.dart'

part of my_lib;

class A{
    //...
}

I have tried this with the import and part of directives reversed and it still won't work, can you not have a class file as part of a library and have imports?


Solution

  • All your imports should go in the file that defines the library.

    Library:

    library my_lib;
    
    import 'something.dart';
    
    part 'a.dart';
    
    class MyLib {
      //...
    }
    

    a.dart

    part of my_lib;
    
    class A {
      //...
    }
    

    Since a.dart is part of my_lib it will have access to any files that my_lib imports.