How to "refresh" dependency packages with main project in Flutter? - flutter

I am new to flutter development. I am trying to separate my code into multiple local packages as dependencies. Here's my current project structure:
/packages/commons: a package containing common widgets & utility functions
/packages/fruits: a package containing screens about fruits (depends upon: commons)
/main: depends upon commons & fruits
Whenever I make a dependency-changeĀ¹ in commons package that affects fruits package, I have to execute flutter pub get in three folders(for commons, fruits and main-project) to be able to run code.
Is there any way to reduce this process to a single "refresh" click?
Example in commons:
flutter pub add fluro
flutter pub get

Since I was getting impatient, I thought of writing a small shell-script for this. I don't know if there is an easier way, but this worked for me.
My project has following folder structure:
project
pubspec.yaml
packages/
package1/
pubspec.yaml
package2/
pubspec.yaml
package3/
pubspec.yaml
I created a refresh.bash file in project folder. Here's how it looks like:
# open project directory or exit if failed
cd PROJECT_PATH || exit
# check if project contains pubspec.yaml file
if [ -f "pubspec.yaml" ]; then
# check if packages folder exists
if [ -d "packages" ]; then
# open packages folder
cd "packages" || exit
# run for all subdirectories of packages folder
for d in */; do
# open subdirectory
cd "$d" || exit
# check if subdirectory contains pubspec.yaml file
if [ -f "pubspec.yaml" ]; then
# run pub get for subdirectory (package)
flutter pub get
fi
# exit subdirectory
cd ..
done
# exit packages directory
cd ..
fi
# run pub get for project directory
flutter pub get
else
echo "pubspec.yaml not found"
fi
Replace PROJECT_PATH with path to your project.
To be able to run this script, I had to make it executable. This can be done with following command:
chmod +x refresh.bash
After this, I ran the script (in bash terminal) with:
./refresh.bash
Note: I don't know how shell-scripts work as well, I took some hints from a Shell Script Cheatsheet. So, if there are any issues, or if it can be smaller, please suggest me. This is my first day with shell-scripting.

Related

snapcraft build subdirectory

I have an flutter based application consisting of serveral "apps" that share some common source code. I tried to setup a snapcraft file for my application, but I can't figure out is make snap build a subdirectory.
This is an example layout that I have
appa/lib/main.dart
appb/lib/main.dart
packages/foundation
This is an example snapcraft file I have
parts:
appa:
source: .
source-type: local
plugin: flutter
flutter-target: lib/main.dart # The main entry-point file of the application
build-snaps: [ yq ]
override-build: |
cd appa
snapcraftctl build
snapcraftctl set-version "$(yq r pubspec.yaml version)"
In the above code I am sharing the the root directory . because otherwise I get an error that flutter cannot get ../packages/. When I build with the root directory I get an error that the working directory isn't a project root and flutter pub get fails.
Most examples I'm able to find online have a easier setup so haven't had any luck deriving a solution for this on my own. I am quite new snapcraft in general and terminology is a bit confusing to me. Would appreciate if somebody can point me in the right direction?

In Flutter when I write Command "flutter build apk" I have error " Error: No pubspec.yaml file found

Error: No pubspec.yaml file found.
This command should be run from the root of your Flutter project.
Do not run this command from the root of your git clone of Flutter.
Please note that I have executed the same project previously more than once.
You are in the wrong folder in the command prompt..navigate to the root directory of your project in which the pubspec.yaml is placed.

Error in terminal while running flutter pub get: Response:

I am new to flutter and SO, I tried this Flutter force higher package dependency version,
My code was
dependencies:
intl: ^0.15.0
I replaced it with
dependency_overrides:
intl: ^0.16.0
But I still get this error
$ flutter pub get
Error: No pubspec.yaml file found.
This command should be run from the root of your Flutter project.
Do not run this command from the root of your git clone of Flutter.
This isn't a pubspec issue. You're simply executing the command from the wrong directory.
cd into the directory where your flutter project files are. That will be the folder that contains other folders like, build, android, ios, lib along with the pubspec.yaml file. Run flutter pub get from there.
Go to the root of your project, where the lib and other directories exist. Then run the command. The best is to use the terminal of your code editor as the path would be same as root of your project in there.

I am not able to use other packages in dependencies in flutter

I have just started using flutter and I am using flutter packages like material.dart , cupertino.dart but when it come to use other packages like image_picker, english_words, I am not able to use it and I had declared packages with versions in dependencies in pubsec.yaml file. After when I write the import package name in the main file it shows a red line under it and the folder was also not created on the left side.
It seems the packages that were declared as dependencies in pubspec.yaml files are not yet downloaded.
Try running: flutter packages get from your project's root directory.
Sometimes it doesn't work even after the above command is executed, in such case close current project and reopen it.

How can I run an example from a Flutter package/library?

I have the following simple directory structure:
flutter_published
.idea
android
build
ios
lib
main.dart
flutter_published.iml
pubspec.lock
pubspec.yaml
network_to_file_image
.idea
example
main.dart
lib
network_to_file_image.dart
test
network_to_file_image.iml
pubspec.lock
pubspec.yaml
network_to_file_image is a package.
There are two main.dart files, one at flutter_published/lib/main.dart and
another at flutter_published/network_to_file_image/example/main.dart
I am able to run the first one, but not the one inside of the example directory under network_to_file_image. The second one gives me this error:
Launching example\lib\main.dart on Android SDK built for x86 in debug mode...
No application found for TargetPlatform.android_x86.
Is your project missing an android\AndroidManifest.xml?
Consider running "flutter create ." to create one.
Also, when the app is generated, what happens to the example and test directories of the packages I use? Are they included or removed from the final app that is deployed?
To solve this, instead of the main.dart file inside of the example directory, you need to create a complete Flutter application-type project inside of the example directory. Then, the example tab will point to the README.md file inside of that directory.
That example directory will have its own lib directory, containing a main.dart file. Since that file is now inside of an application-type directory it can be run.
Visit this repo to see how it works:
https://github.com/marcglasberg/async_redux/tree/master/example
Update:
To be clear, the example's pubspec.yaml file can reference its package by using a relative reference. For example, here is the dependencies section of the example dir of the async_redux package I mentioned:
dependencies:
http: ^0.13.1
async_redux:
path: ../
flutter:
sdk: flutter
Since the example dir is at the same level as the package's pubspec.yaml file, then the example's own pubspec.yaml is one level below it. Thus, it may reference the package itself by using a ../ path:
async_redux:
path: ../
example/main.dart only exists to be shown in https://pub.dartlang.org/packages/network_to_file_image#-example-tab-
The pub site is limited in how it finds content in the example directory to display in the Example tab.
cd to the directory and execute flutter create .. You should be able to run it afterwards
Go to the example folder of the repository of the package.
Open the lib folder and go to the main.dart file.
Above the void main() function, you can see the Run|Debug|Profile (pic below), click on the one you want to run the project as.
The example app will now run in your emulator.
The screenshot below is of the chewie package available on pub.dev