I have Dart only project.
How to print the package name ?
For example:
package:myapp/something.dart
How to print this "myapp" from the Dart program itself ?
In other word, I am reffering to the first line of pubspec.yaml name: myapp
Tried
Platform.script; // => /absolute/path/to/the/script.dart
Directory.current; // => /absolute/path/to/the/working/directory
I found a solution with Dart Mirror using
class DummySelf {}
Then
String currentPackage () {
final classMirror = reflectClass(DummySelf);
final b = classMirror.location;
final c = b.toString().replaceFirst("package:", "").split("/").first;
return c;
}
But it is a very very very ugly solution, unless someone confirms it is the only solution
2
Answers
add
yaml
package in project and with the following code you can access the content of the pubspec.yamlThere’s a package that does what your looking for, it’s called package_info_plus
You can use it like this:
import ‘package:package_info_plus/package_info_plus.dart’;