Dart command line app on Windows, can't import packages - command-line

I'm on Windows 10 and I'm following this tutorial: https://www.dartlang.org/docs/tutorials/cmdline/
However, it seems it cannot use packages as per below output. Am I missing something, or is this a known issue on Windows? I installed dart using choco and it added dart's location to my path.
I was able to make it work by copying the packages junction (symbolic link or shortcut maybe?) that WebStorm created for some project and pasting it next to this dart file. But I don't think it's what we're supposed to do.
How can we make sure that dart command line apps in Windows can use packages? What is missing in the tutorial link that I gave above?
C:\Development\Dartish\tmp>dir
Volume in drive C is HDD
Volume Serial Number is DC07-D177
Directory of C:\Development\Dartish\tmp
06/01/2016 03:28 PM <DIR> .
06/01/2016 03:28 PM <DIR> ..
06/01/2016 03:33 PM 2,063 dcat.dart
1 File(s) 2,063 bytes
2 Dir(s) 74,771,554,304 bytes free
C:\Development\Dartish\tmp>dart dcat.dart -n quotes.txt
Unhandled exception:
Could not import "package:args/args.dart" from "file:///C:/Development/Dartish/tmp/dcat.dart": Could not resolve a package location for base at file:///C:/Development/Dartish/tmp/dcat.dart
#0 _asyncLoadErrorCallback (dart:_builtin:162)
#1 _asyncLoadError (dart:_builtin:584)
#2 _loadPackage (dart:_builtin:625)
#3 _loadPackage.<anonymous closure> (dart:_builtin:641)
#4 _handlePackagesReply (dart:_builtin:482)
#5 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)
C:\Development\Dartish\tmp>

You definitely should not need to manually copy symbolic links or junctions.
If you run pub get or pub upgrade from the directory that contains the pubspec.yaml file or from within WebStorm then all these junctions should be created for you and shouldn't be modified in any way manually.
You need a pubspec.yaml file in the tmp directory
name: some_name
dependencies:
args: any # any is discouraged but should work in this case
run pub get after the file was created
dcat.dart should be in tmp/bin
Then run from the tmp directory
dart bin/dcart.dart -n quotes.txt

Related

How to "refresh" dependency packages with main project in 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.

Bad: Flutter downloading problems

So I extracted the files in the C: drive (not the program files) and added C:\flutter\src\bin in the Path variable and when I run flutter --version in cmd it gives an error..please help
It could be great to see what the error is.

How to run flutter_web and angulardart on one machine with multiple Dart and webdev versions?

How to run flutter_web and angulardart on one machine with multiple Dart and webdev versions? Even if I set a different PATH when I need to switch Dart versions, two Dart versions are still using the same webdev which is not really correct.
(There is no official solution for this so far. Maybe the official team will provide one in the future.)
The following is my solution. I am running MacOS. It should be the same in Linux and similar in Windows.
There are two things need to be taken care. (1) Dart SDK version (2) A different PUB_CACHE because webdev versions will be different.
I download the dart version required by angulardart manually from this link and put it somewhere NOT in the PATH. And put the dart for flutter and flutter_web in the PATH. (I will not explain this in detail. It should be pretty straightforward.)
Then I write three scripts and one env setup file:
File angular-env:
export PUB_CACHE="$HOME/.pub-cache-angular"
export PATH="Override PATH"
Note: when you override PATH, make sure (1) Remove flutter dart bin, (2) Remove ${HOME}/.pub-cache/bin (3) add angulardart dart bin, (4) add $HOME/.pub-cache-angular/bin
File angular-dart:
#!/bin/bash
source angular-env
dart "$#"
File angular-pub:
#!/bin/bash
source angular-env
pub "$#"
File angular-webdev:
#!/bin/bash
source angular-env
webdev "$#"
Don't forget to make them executable.
Now, you can simply use angular-pub global activate webdev to install webdev for angulardart.
After this, everything should work. You can run angular-webdev serve in the angulardart project.
Check some of my terminal outputs:
$ dart --version
Dart VM version: 2.5.0-dev.1.0.flutter-fedd74669a (Sat Jul 20 00:05:23 2019 +0000) on "macos_x64"
$ angular-dart --version
Dart VM version: 2.2.1-dev.3.0 (Tue Apr 2 13:55:04 2019 +0200) on "macos_x64"
$ pub version
Pub 2.5.0-dev.1.0.flutter-fedd74669a
$ angular-pub version
Pub 2.2.1-dev.3.0
$ webdev --version
2.3.0
$ angular-webdev --version
2.0.5

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

How to remove or identify unused packages from flutter to reduce size of the project?

I used some packages that I no longer need in my flutter project, namely the wilddog_auth and wilddog_sync, I can remove the imports from pubspec.yaml file, and my dart files (aka removing import 'package:wilddog_sync/wilddog_sync.dart' etc.) and remove imports in MainActivity.java as well as in Xcode project but I can't purge the unused files installed by flutter, cocoapod and gradle. Now is there a unified command in flutter where I can remove all unused packages at once?
I am pretty sure using flutter clean only removes build folder and using flutter packages get after removing packages from pubspec.yaml doesn't remove packages from cocoapod or gradle either.
For example, after flutter clean and flutter packages get I rebuilt the project:
Launching lib/main.dart on Android SDK built for x86 64 in debug mode...
Initializing gradle...
Resolving dependencies...
Running 'gradlew assembleDebug'...
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...
I/FlutterActivityDelegate(11331): onResume setting current activity to this
I/Choreographer(11331): Skipped 107 frames! The application may be doing too much work on its main thread.
D/EGL_emulation(11331): eglMakeCurrent: 0x79f21b4dbec0: ver 2 0 (tinfo 0x79f219b4f160)
I/OpenGLRenderer(11331): Davey! duration=1903ms; Flags=1, IntendedVsync=7544454683637, Vsync=7546238016899, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=7546253803603, AnimationStart=7546253996603, PerformTraversalsStart=7546254171603, DrawStart=7546279222603, SyncQueued=7546288526603, SyncStart=7546295569603, IssueDrawCommandsStart=7546296644603, SwapBuffers=7546334455603, FrameCompleted=7546365204603, DequeueBufferDuration=2892000, QueueBufferDuration=199000,
Syncing files to device Android SDK built for x86 64...
D/ (11331): HostConnection::get() New Host Connection established 0x79f219ab81e0, tid 11383
D/EGL_emulation(11331): eglMakeCurrent: 0x79f21b5e26e0: ver 2 0 (tinfo 0x79f219aa66c0)
W/IInputConnectionWrapper(11331): getCursorCapsMode on inactive InputConnection
Yet all the package files remained.
Of course I could go into ./ios/ to run pods install to remove pods:
pod install
Analyzing dependencies
Fetching podspec for `Flutter` from `.symlinks/flutter/ios`
Removing Wilddog
Removing WilddogCore
Removing WilddogSync
Downloading dependencies
Using Flutter (1.0.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Automatically assigning platform `ios` with version `8.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
There is an intuitive way for it.
Install this package
dev_dependencies:
dependency_validator: ^x.x.x
Then run flutter pub run dependency_validator
It will show all the details about the packages installed.
Example: =>
These packages are pinned in pubspec.yaml:
dependency_validator: null -- This is a direct pin.
permission_handler: null -- This is a direct pin.
swipe_to: null -- This is a direct pin.
These packages are used in lib/ but are not dependencies:
connectivity
meta
path
The following packages contain executables, they are assumed to be used:
flutter_native_splash
These packages may be unused, or you may be using assets from these packages:
cupertino_icons
firebase_analytics
http
loading
sign_in_with_apple
delete the hosted folder of .pub-cache in flutter SDK directory
cd path/to/flutter_sdk_directory
rm -rf .pub-cache/hosted
delete the .packages file in the project root directory
cd path/to/project_root_directory
rm -rf .packages
get packages for the project
cd path/to/project_root_directory
flutter pub get
open the project in Android Studio
done.
Sometimes there might be packages that is not used in the project but you have installed sometime during the development stage. To find out those, First go to each dart file and remove the unused imports, then go to pubspec.yaml and comment the packages one by one and save the project. And then refresh the application(NOT HOT RELOAD). if the package us used in the project then it will throw you an error. After repeating the steps on all the packages you will be able to remove all the unwanted packages from your pubspec.
Now, delete the pubspec.lock file, and run the pub get again. Your project will now be having only the required packages and this could be the minimum size achievable.
There's an obvious way which is easy to overlook or think is not feasible.
If using IntelliJ, simply do "Find in files" (SHIFT+CTRL+F on PC, SHIFT+CMD+F on Mac), select "Directory" and choose the lib-folder of your project.
Then go through your list of plugins and search for the name of each one in turn. The plugin isn't used if
there are no search results for its name
it's only used in "generated_plugin_registrant.dart"
the only hits are import statements that are grey (marked as unused by IntelliJ)
Took me less than a minute to eliminate 8 unused plugins using this method.
You can use this three steps to clear this issue
'flutter clean'
Remove the unwanted dependencies in pubspec.yaml
Delete the pubspec.lock
Reload the project and again perform 'pub get' and run the iOS/Android project
This behavior seems to be a bug. You can track it here https://github.com/flutter/flutter/issues/65718
The best way to do this is
flutter clean
If this doesn't work just create a new project copy your old code into the new project and here you can add only the dependencies you want before running pub get.
You can remove by typing in terminal dart pub remove <packagename>
Example : To remove flutter_svg:^0.22.0 ,
type dart pub remove flutter_svg
For more informations, check https://dart.dev/tools/pub/cmd/pub-remove