10
votes

For this question to make any sense at all I'm going to explain my current project structure first. For the company I'm working for I've made a platform to build apps quickly and customizable for every customer we have. So the project works with 2 repositories.

1) The app repository
This repository contains all the customizable files for the app. It is basically the entire flutter project without the lib and test directory.

2) The core repository
For the app to be functional the core repository is loaded in the app repository. (This contains all the .dart files). We did it this way because if we make a change to the core it is updated in all the apps we're creating by doing a simple git pull on the core.

Now comes the part that I can't figure out. We now have a customer that wants a custom functionality in the app. We cannot use this functionality again so I wanted to add that piece of code to the app repository. This is the file structure (Only showing what's needed)

Project
- assets
- customLib (the files for the custom work for the customer, part of the app repo)
- lib (all the files in this directory come from the core repo)
- test (all the files in this directory come from the core repo)
- pubspec.yaml

in one of my files in the core app I'm loading in the file.
This is the file /lib/Library/ModuleRouter.dart
Loading it like this: import '../../customLib/customLibs.dart';
It does not matter what I try but when I go outside the scope of where main.dart is placed the code simply isn't found. The files exist though.

This is the error I'm getting from the compiler:

Compiler message:
Error: Invalid package URI 'package:/customLib/customLibs.dart':
  Invalid argument (packageUri): Package URIs must not start with a '/': Instance of '_SimpleUri'.
lib/Library/ModuleRouter.dart:25:8: Error: Not found: 'package:/customLib/customLibs.dart'
import '../../customLib/customLibs.dart';

Sorry for the long post but I felt like explaining the whole project for a better understanding of the current project.

I hope someone can help me with this issue.

Kind regards,
Kevin Walter

2

2 Answers

6
votes

Importing files from outside lib/ is not supposed to work. This would prevent publishing packages to pub.dartlang.org because only lib/ and bin/ are downloaded when added to dependencies.

What you can do is to create a new package, that can even be located in a directory of another package (for example my_proj/example/example1) or outside of the project and then add it as a dependency to your primary project and import files from that package using package imports like import "package:example1/example1.dart".

I'd discourage nesting packages except for purposes like example.
Having closely related packages as siblings is usually the better approach.

See also

0
votes

The accepted answer is the correct answer.
In my case I still needed to import a single file. (it was for debug purposes only)

import 'file:///Users/uesr_name/Desktop/flutterPlugin/flutterPlugin/lib/file.dart';