flutterdartpackagevisibility

Flutter package class file visibility in project


I have created a Flutter package and I am using it in my Flutter project.

The package has a class FirebaseAuthFlowState located in ...core/firebase_auth_flow_state.dart. A package's "root" file with an export is located right in a lib directory and exports FirebaseAuthFlowState.

When I am going to use the class FirebaseAuthFlowState in my Flutter project, my IDE (VS Code) whispers me two possible imports, both works:

  1. package:firebase_auth_flow/core/firebase_auth_flow_state.dart
  2. package:firebase_auth_flow/firebase_auth_flow.dart

screenshot

My firebase_auth_flow.dart file
library firebase_auth_flow;

export 'package:firebase_auth_flow/core/firebase_auth_flow_state.dart'
    show FirebaseAuthFlowState;

Solution

  • I'm not entirely sure, but I believe you should put files in lib/src/ or a subdirectory of it to exclude them from the import suggestions.

    The way I figured this is because I looked how flutter itself does it for its classes. I took ListView as example and when trying to use it it has these suggestions:

    enter image description here

    After choosing one of the imports, when Ctrl+clicking on ListView I found out ListView is actually located at

    <my_flutter_directory>\packages\flutter\lib\src\widgets\scroll_view.dart
    

    So I thought for myself "Can I actually directly import it from there?" and the answer is yes. I removed the suggested import and wrote this instead:

    import 'package:flutter/src/widgets/scroll_view.dart';
    

    And this works as expected although the IDE gives the following warning

    Import of a library in the 'lib/src' directory of another package.
    

    Which makes me think that it's not normal to import from there and also why it doesn't show up in the suggestions, so that's probably the way to do it.