how to modify a dart/flutter package? - flutter

I want to use a dart package. Currently its under dependencies in pubspec.yaml. And it works fine. However I need to make a few minor changes to the package. Do I need to import the whole package into my project and make changes there or is there an easier way?

You should clone the package from it's own repository and then just import it using the path instead. For example, if you have let's say, a package called foo: ^1.0.0 and want to modify it:
Go to it's GitHub project and clone it to somewhere in your machine;
Change the path in your pubspec.yaml to:
foo:
path: [your package path]
Have in mind that if you have the project stored in VC, the reference won't work and thus you should always point to a remote dependency unless it's shipped with your app as well (eg. path: ../dependency).

Related

How to share class between two projects in dart/flutter?

I have on project on pure dart (the server).
And another one in flutter (the client).
How can I easily share the folder lib/model with my another project?
I want to use the same collection of class in both projects.
Create a dart package which can be used in both server and client.
$ dart create -t package <PACKAGE_NAME>
See Creating packages for details.
The package can be published, used directly from git or used locally (e.g. mono repo), see Using packages.
I can do this by
A. I create a folder test_project
B. In the test_project folder, I create the dart server project by using dart create test_project_server and create the folder to share anything that I want by using dart create test_project_share.
Now you have folders/projects test_project_server and test_project_share in the test_project folder.
C. Open the project test_project_share and create the file share_class.dart in the lib folder. Copy my example class and paste this code into this file.
class ShareClass {
static String value = "Hello World";
}
D. Open the test_project_server project and add this code below the dependencies of the pubspec.yaml file.
test_project_server:
path: ../test_project_server
E. Run pub get in the test_project_server terminal.
Now, you can call ShareClass from test_project_share to the test_project_server project. Hope this will help you.

is it possible to compile the flutter library

I am write a flutter public lib, before I commit the code to the repo, I want to compile the whole project. Sometimes I want to make sure all code could compile, for example, after you changed the function the IDE may not give obviously tips somewhere going wrong, is it possible to compile the project? when I am developing a app I could run it, but when I develop a public library I do not know how to run it and check the whole project.
When making a public package, you should include an example project within it. It's basically a new flutter project that sits inside the pub project, typically under "example" folder. You can take a look at one of my packages here on GitHub, to get an idea of the folder structure.
If you click into example folder, you will see a complete Flutter project on its own. It has its own pubspec.yaml as well, and refers to the "parent folder" to use the pub package as a demo.
dependencies:
flutter:
sdk: flutter
animated_flip_counter:
path: ../
Check the docs about writing packages. https://docs.flutter.dev/development/packages-and-plugins/developing-packages

Overwrite files in nuget

I have a nuget package that references other nuget packages. One of these "inner" packages has a security hole. How can I - without changing the package - overwrite the "inner" reference? Thanks for the help!
Your only option is to directly reference that package in your project/package so that the fixed version is "nearer" to the project that is being restored.
However you cannot change the package identifier, only the version being used (and in worst case szenarios, create your own package from a fork with the same id/name but a custom version - e.g. 1.2.3-workaround.1)

How to add a second example app to a Flutter plugin project?

When creating a plugin project in Flutter, an example app, that is using the plugin, is added in a subfolder of the plugin project. What needs to be done to add a second "example" app to the plugin folder?
So far I have:
Copied and renamed the example folder to (let's call it) app2.
Adjusted the package names at the android manifest files of app2.
Renamed pluginrootfolder/app2/pluginname_example.iml to pluginrootfolder/app2/pluginname_app2.iml (to reflect the name of the second app).
In the .iml file of the plugin project (the root folder):
Copied and adjusted the exclude folder directive to reflect app2
<excludeFolder url="file://$MODULE_DIR$/app2/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/app2/.pub" />
<excludeFolder url="file://$MODULE_DIR$/app2/build" />
Run Flutter clean and Flutter pub get in both the plugins root directory and app2's directory.
Problem now is that app2 causes 1k+ Target of URI doesn't exist: error messages - from packages (like provider and json_annotation) to classes in the plugin's root project.
Do you have any ideas what's wrong here or how to fix it?
Firstly, please give a full GitHub reproducible repository and link it here so I can examine. And also paste (e.g. using github gist) the long log. Please comment to this answer so I can examine more.
"In the .iml file of the plugin project (the root folder)" - indeed not that needed. iml is for intellij idea, and is unrelated to flutter. so should not error even with wrong iml. You can even safely delete iml and when you import that module in intellij it will auto recreate.
Have you modified pubspec.yaml? e.g. the package name in it.
And, where is your plugin located, relative to your app2? In example app, the default pubspec.yaml uses path: ../ to point to your plugin. So if your app2 does not have .. as the plugin path you should change this as well.
Anyway, paste your log and give a reproducible sample please...
I suspect your issue is in the dependency tree. Your example2 project does not seem to point to the project above it. The only manual edits you need are in the pubspec.yaml. In the dependencies for the second example project.yaml file, include the path to the root project. For example:
dependencies:
<YOUR_ROOT_PROJECT>:
path: ../
flutter:
sdk: flutter
Including that should map the decencies of the root over with pub.get.
That should take care of the Target of URI doesn't exist errors.
Since you are using .iml files, I assume you're using an IntelliJ/JetBrains IDE. Creating example subprojects can be done without all of the manual work you listed.
You don't have to copy the files themselves manually to create a second example. You can simply right-click on the project root and select new module. Choose Flutter. The project type is Application.
Secondly, you don't have to add the second project to the exclude folders.

Can we create shared library or assembly like .dll in flutter package project?

I'm getting problem with flutter/dart. I created one flutter-package using dart lang, there is so many dart files. But i didn't get any option to make single lib file or assembly like dll (dll in Xamarin). I want to use that API in flutter mobile app [Android/iOS]. So is it possible to make single lib file in dart ? I don't want to show a source code to that person, who will use my API in Android/iOS & I don't want to publish my code to pub.dev.
I hope this is possible.
Thanks, i would be grateful.
You can store your package on your git repository and access them.
If the package is located at the root of the repo, use the following syntax:
dependencies:
plugin1:
git:
url: git://github.com/test/plugin1.git
https://flutter.dev/docs/development/packages-and-plugins/using-packages