Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to give a different name to a package dependency in pubspec

Tags:

dart

dart-pub

I managed to add a relative path dependency in my app

name: myapp
description: A sample app
dependencies:
  mylib:
    path: ../mylib

and then import it in my source code

import 'package:mylib/mylib.dart';

However, the name 'mylib' is taken from the library package pubspec and if I want to change it (for example 'mynewlib'), I have to change the name everywhere (pubspec AND dart source code)

It also prevent having 2 packages with the same name (yes I know, weird, but I don't control what people put in pub.dartlang.org). What I'd like to do is something like

name: myapp
description: A sample app
dependencies:
  mylib:
    path: ../mylib
    name: mynewlib

and have in source code

import 'package:mynewlib/mylib.dart';

However I cannot find the proper syntax and whether that's possible or not. (Sample code ready for testing is here: https://github.com/alextekartik/dart-test/tree/master/lib_test). To note that here I'm not talking about library name but package name (and naming the package mylib can be confusing)

like image 891
alextk Avatar asked Dec 13 '25 14:12

alextk


1 Answers

There is no way to define another name for a package itself (as far as I know - if there is, I'd be interested too).

However, as a workaround, you could rewrap it. For example, let's assume you have two "mylib" packages.

Create a new library application "mylib1". There, you import the first "mylib" and reexport it using export. Create another library application "mylib2" for the other "mylib". Then you have different package names to use in the same application.

Yes, it is kinda awkward, but as long as there is no better way...

like image 82
MarioP Avatar answered Dec 15 '25 04:12

MarioP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!