Jacques
Jacques

Reputation: 319

Own common libraries in own dart/flutter projects in VSCode?

I'm working on one project for some time now on flutter. Part of the source code has been designed so that it can be used again as is in other projects.

I'm working with Visual Studio Code.

Now I'm creating a second project. I'd like to organize folders this way:

Parent folder
   Project1 folder
   Project2 folder
   my_library

Is it possible to add the library folder to the projects, as it is not inside their respective folders?

Upvotes: 0

Views: 407

Answers (2)

Jacques
Jacques

Reputation: 319

The way to solve this isn't straightforward for beginners. So I summed up the proposed solutions here (I provide the names out of fairness, and follow related potential discussions below each one).

  1. From Richard Heap:

In pubspec.yaml of project 1, refer to the library as:

dependencies:
  my_library:
    path: ../my_library
  1. From me:

In ../my_library, add a specific pubspec.yaml. Something like:

name: my_libraries
description: my own common libraries

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.17.1 <3.0.0"
  
dependencies:
  flutter:
    sdk: flutter
  1. From me:

Drag and drop (maybe there's a menu) your my_libraries folder from a file explorer to the VSCODE Explorer panel, and choose "add folder to workspace". Of course, all dependencies to external libraries used in my_libraries must be specified in its own pub_spec.yaml file.

Remarks:

  • I added source files in my_libraries/lib/. Don't know if the lib sub-directory is mandatory. Didn't take time to test without, and I like better to keep the same structure in my_libraries than in projects.

  • in project (and my_library) source files, to import my_library source files, just do as:

    import 'package/my_library/xxx.dart'

where xxx.dart is the file to import.

Upvotes: 0

Richard Heap
Richard Heap

Reputation: 51750

In pubspec.yaml of project 1, refer to the library as:

dependencies:
  my_library:
    path: ../my_library

Upvotes: 1

Related Questions