Whats the difference between dart run vs flutter pub run - flutter

I downloaded the Mockito library (https://pub.dev/packages/mockito) and followed the instruction to use "dart run build_runner build" to generate mocks in my project directory and it failed with the following error.
Could not find a file named "pubspec.yaml" in "/Users/mark/.pub-cache/hosted/pub.dartlang.org/_fe_analyzer_shared-31.0.0".
#0 new Pubspec.load (package:pub/src/pubspec.dart:288:7)
#1 new Package.load (package:pub/src/package.dart:149:27)
#2 SystemCache.load (package:pub/src/system_cache.dart:113:20)
#3 Entrypoint._createPackageGraph (package:pub/src/entrypoint.dart:112:63)
#4 Entrypoint.packageGraph (package:pub/src/entrypoint.dart:107:54)
#5 getExecutableForCommand (package:pub/src/executable.dart:338:19)
#6 RunCommand.run (package:dartdev/src/commands/run.dart:232:32)
#7 CommandRunner.runCommand (package:args/command_runner.dart:209:27)
#8 DartdevRunner.runCommand (package:dartdev/dartdev.dart:232:30)
#9 CommandRunner.run.<anonymous closure> (package:args/command_runner.dart:119:25)
#10 new Future.sync (dart:async/future.dart:296:31)
#11 CommandRunner.run (package:args/command_runner.dart:119:14)
#12 runDartdev (package:dartdev/dartdev.dart:67:29)
#13 main (file:///opt/s/w/ir/cache/builder/sdk/pkg/dartdev/bin/dartdev.dart:11:9)
#14 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:293:32)
#15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
However if I followed the instructions in Flutter's cookbook(https://docs.flutter.dev/cookbook/testing/unit/mocking#3-create-a-test-file-with-a-mock-httpclient) and used "flutter pub run build_runner build" in the project directory it works.
What are the differences between dart run and flutter pub run? Why dart run tries to execute from the /Users/username/.pub-cache directory when I am executing it from the project directory?
I saw this related issue in Mockit, however it does not explain the differences. (https://github.com/dart-lang/mockito/issues/435)

Related

Flutterfire configure Unexpected character (at character 1) error

I am developing a project with flutter and i want to add crashlytics to my project. Everything else works but flutterfire configure is giving me json error. I look every where for all possible answers but still not solving the issue.
npm commands
- firebase login
- npm install -g firebase-tools
- dart pub global activate flutterfire_cli
- flutterfire configure
all these commands is also works in android studio terminal. but flutterfire gives json error.
I already added the android dependencies for firebase.
error log
⠏ Fetching available Firebase projects...
Unhandled exception:
FormatException: Unexpected character (at character 1)
#
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#4 JsonDecoder.convert (dart:convert/json.dart:612:36)
#5 runFirebaseCommand (package:flutterfire_cli/src/firebase.dart:95:25)
<asynchronous suspension>
#6 getProjects (package:flutterfire_cli/src/firebase.dart:114:20)
<asynchronous suspension>
#7 ConfigCommand._selectFirebaseProject (package:flutterfire_cli/src/commands/config.dart:278:24)
<asynchronous suspension>
#8 ConfigCommand.run (package:flutterfire_cli/src/commands/config.dart:368:37)
<asynchronous suspension>
#9 CommandRunner.runCommand (package:args/command_runner.dart:209:13)
<asynchronous suspension>
#10 main (file:///C:/Users/USER/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/flutterfire_cli-0.2.4/bin/flutterfire.dart:57:5)
<asynchronous suspension>
⠧ Fetching available Firebase projects...
Unhandled exception:
FormatException: Unexpected character (at character 1)
#
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1250:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:915:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#4 JsonDecoder.convert (dart:convert/json.dart:612:36)
#5 runFirebaseCommand (package:flutterfire_cli/src/firebase.dart:95:25)
<asynchronous suspension>
#6 getProjects (package:flutterfire_cli/src/firebase.dart:114:20)
<asynchronous suspension>
#7 ConfigCommand._selectFirebaseProject (package:flutterfire_cli/src/commands/config.dart:278:24)
<asynchronous suspension>
#8 ConfigCommand.run (package:flutterfire_cli/src/commands/config.dart:368:37)
<asynchronous suspension>
#9 CommandRunner.runCommand (package:args/command_runner.dart:209:13)
<asynchronous suspension>
#10 main (file:///C:/Users/USER/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/flutterfire_cli-0.2.4/bin/flutterfire.dart:57:5)
<asynchronous suspension>
Would you please help me to solve this issue?
For those of us on windows, do not forget to check if you are running an old version which you may have configured the PATH to point to. This seemed to be the problem in my case. I resolved by removing the old windows binary, then did a fresh install using npm (a fresh/updated windows install may work too). :
npm install -g firebase-tools
followed by:
firebase logout
firebase login
then:
flutterfire configure
**** update ****
i resolve error by this step :
1 - re connect to network
2 - run dart pub global activate flutterfire_cli
3- run flutterfire configure
see Docs help you to get started
Add Firebase To Flutter App
hope this help you
same error here i used firebase in another project but in this project it give me this
Unhandled exception:
FormatException: Unexpected character (at line 5, character 1)
{
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:848:48)
#2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3 JsonDecoder.convert (dart:convert/json.dart:612:36)
#4 runFirebaseCommand (package:flutterfire_cli/src/firebase.dart:95:25)
<asynchronous suspension>
#5 getApps (package:flutterfire_cli/src/firebase.dart:162:20)
<asynchronous suspension>
#6 findOrCreateFirebaseApp (package:flutterfire_cli/src/firebase.dart:257:34)
<asynchronous suspension>
#7 FirebaseAndroidOptions.forFlutterApp (package:flutterfire_cli/src/firebase/firebase_android_options.dart:47:25)
<asynchronous suspension>
#8 ConfigCommand.run (package:flutterfire_cli/src/commands/config.dart:377:24)
<asynchronous suspension>
#9 CommandRunner.runCommand (package:args/command_runner.dart:209:13)
<asynchronous suspension>
#10 main (file:///home/abdalazeez/.pub-cache/hosted/pub.dartlang.org/flutterfire_cli-0.2.4/bin/flutterfire.dart:57:5)
<asynchronous suspension>
and this content file
try {
final flutterApp = await FlutterApp.load(Directory.current);
await FlutterFireCommandRunner(flutterApp).run(arguments); <=====the error happen here
} on FlutterFireException catch (err) {
if (utils.activeSpinnerState != null) {
try {
utils.activeSpinnerState!.done();
} catch (_) {}
}
stderr.writeln(err.toString());
exitCode = 1;
} catch (err) {
exitCode = 1;
rethrow;
}
Try installing the Firebase CLI using the standalone binary instead of NPM on Windows, or the install script on Mac.
following steps helped me:
update firebase cli
curl -sL firebase.tools | upgrade=true bash
call
firebase login --reauth
reference:
https://github.com/invertase/flutterfire_cli/issues/27
downloading and installing the node.js setup does the trick for me,
after installation, search for the node.js terminal , then run 'firebase login', after login successfully, then run 'dart pub global activate flutterfire_cli', after succesfully getting the flutterfire cli, then navigate to the folder where the flutter app you created is using the cd(change directory)
after navigating to the directory, then run 'flutterfire configure'
this would create a firebase project in your flutter app project. finish
install latest version of npm and then run command of flutter fire.
example: from any directory run: npm install -g npm#latest version
=> npm install -g npm#9.2.0
as of now latest version of npm is 9.2.0
Deleting the C:\Users\USERNAME.cache\firebase folder
Then re-running flutterfire configure worked for me after first trying the standalone binary and then nvm.

LateInitializationError: Field 'annotationAst' has not been initialized

I upgrade my packages using dart pub outdated --mode=null-safety command. after updating my packages I got this error while running dart migrate to migrate my code to null-safety.
also when I tried to run flutter pub run build_runner build --delete-conflicting-outputs command for moor database.
I have no clue where this error is coming from and my codebase is really big to search for any initialization or annotation
Why does this issue happen and How can I solve that?
LateInitializationError: Field 'annotationAst' has not been initialized.
#0 ElementAnnotationImpl.annotationAst (package:analyzer/src/dart/element/element.dart)
#1 ConstantEvaluationEngine.computeDependencies (package:analyzer/src/dart/constant/evaluation.dart:336:39)
#2 _ConstantWalker._computeDependencies (package:analyzer/src/dart/constant/compute.dart:75:22)
#3 _ConstantNode.computeDependencies (package:analyzer/src/dart/constant/compute.dart:43:19)
#4 Node.getDependencies (package:analyzer/src/summary/link.dart:148:40)
#5 DependencyWalker.walk.strongConnect (package:analyzer/src/summary/link.dart:53:40)
#6 DependencyWalker.walk (package:analyzer/src/summary/link.dart:119:18)
#7 computeConstants (package:analyzer/src/dart/constant/compute.dart:26:14)
#8 LibraryAnalyzer._computeConstants (package:analyzer/src/dart/analysis/library_analyzer.dart:250:5)
#9 LibraryAnalyzer.analyzeSync (package:analyzer/src/dart/analysis/library_analyzer.dart:135:5)
#10 LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:103:12)
#11 AnalysisDriver._computeAnalysisResult.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1624:63)
#12 PerformanceLog.run (package:analyzer/src/dart/analysis/performance_logger.dart:32:15)
#13 AnalysisDriver._computeAnalysisResult (package:analyzer/src/dart/analysis/driver.dart:1600:20)
#14 AnalysisDriver._computeErrors (package:analyzer/src/dart/analysis/driver.dart:1678:26)
#15 AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:1301:20)
#16 AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2334:24)

Dart Null-Safety - Range Error when executing dart migrate to upgrade to new null safety

I'm having problems upgrading a custom package to null safety and am not finding any solutions or even the same problem when searching on here and any other platform.
When upgrading a custom package for Flutter 2.0 null-safety I'm using dart migrate as the guide proposes, but am getting a compiler Error.
But the error does not specify where it occurs nor is the errors description any help. I looked through the entire package to find any Array/List or inclusive range error, without any success.
I'm running out of ideas to fix this error and complete the upgrade to null-safety. Does someone have an idea what I can try to fix this error?
Error message:
dart migrate
Migrating file_name
See https://dart.dev/go/null-safety-migration for a migration guide.
Note: more than one project found; migrating the top-level project.
Analyzing project...
[--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------]No analysis issues found.
Generating migration suggestions...
[--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------]
Compiling instrumentation information...
[--------------------| ]RangeError (index): Invalid value: Not in inclusive range 0..12: 13
#0 List.[] (dart:core-patch/array.dart:268:52)
#1 BestPracticesVerifier._targetKindsFor (package:analyzer/src/error/best_practices_verifier.dart:1582:38)
#2 BestPracticesVerifier.visitAnnotation (package:analyzer/src/error/best_practices_verifier.dart:250:17)
#3 AnnotationImpl.accept (package:analyzer/src/dart/ast/ast.dart:280:49)
#4 NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
#5 AnnotatedNodeImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:148:17)
#6 ClassDeclarationImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:1559:11)
#7 RecursiveAstVisitor.visitClassDeclaration (package:analyzer/dart/ast/visitor.dart:707:10)
#8 BestPracticesVerifier.visitClassDeclaration (package:analyzer/src/error/best_practices_verifier.dart:320:13)
#9 ClassDeclarationImpl.accept (package:analyzer/src/dart/ast/ast.dart:1536:49)
#10 NodeListImpl.accept (package:analyzer/src/dart/ast/ast.dart:7611:20)
#11 CompilationUnitImpl.visitChildren (package:analyzer/src/dart/ast/ast.dart:2141:21)
#12 RecursiveAstVisitor.visitCompilationUnit (package:analyzer/dart/ast/visitor.dart:731:10)
#13 CompilationUnitImpl.accept (package:analyzer/src/dart/ast/ast.dart:2134:49)
#14 LibraryAnalyzer._computeHints (package:analyzer/src/dart/analysis/library_analyzer.dart:281:10)
#15 LibraryAnalyzer.analyzeSync.<anonymous closure> (package:analyzer/src/dart/analysis/library_analyzer.dart:167:9)
#16 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:397:8)
#17 LibraryAnalyzer.analyzeSync (package:analyzer/src/dart/analysis/library_analyzer.dart:166:13)
#18 LibraryAnalyzer.analyze (package:analyzer/src/dart/analysis/library_analyzer.dart:107:12)
#19 AnalysisDriver._computeResolvedLibrary2.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1416:65)
#20 PerformanceLog.run (package:analyzer/src/dart/analysis/performance_logger.dart:32:15)
#21 AnalysisDriver._computeResolvedLibrary2 (package:analyzer/src/dart/analysis/driver.dart:1402:20)
#22 AnalysisDriver._computeResolvedLibrary.<anonymous closure> (package:analyzer/src/dart/analysis/driver.dart:1395:15)
#23 _rootRun (dart:async/zone.dart:1354:13)
#24 _CustomZone.run (dart:async/zone.dart:1258:19)
#25 _runZoned (dart:async/zone.dart:1788:10)
#26 runZoned (dart:async/zone.dart:1710:10)
#27 NullSafetyUnderstandingFlag.enableNullSafetyTypes (package:analyzer/dart/element/null_safety_understanding_flag.dart:42:12)
#28 AnalysisDriver._computeResolvedLibrary (package:analyzer/src/dart/analysis/driver.dart:1394:40)
#29 AnalysisDriver.performWork (package:analyzer/src/dart/analysis/driver.dart:964:22)
#30 AnalysisDriverScheduler._run (package:analyzer/src/dart/analysis/driver.dart:2018:24)
<asynchronous suspension>
This appears to be a bug in the Dart SDK itself. This is tracked in dart-lang/sdk#46183 and was fixed in this commit.
Steps to try on your end:
Run flutter upgrade to be sure you're on the latest release.
If you are still encountering the issue after that, consider changing to Flutter's beta channel to get a more bleeding-edge SDK in order to run the migration tool, then switch back to the stable channel once you're done. You can do this by running flutter channel beta, then re-running the migration, then flutter channel stable to switch back.
I had the same issue, it is fixed in beta channel as stated in the cbracken's answer.
Also, in my case I had second Dart SDK (2.12) installed outside Flutter, so the issue appeared to be unresolved.
Make sure you are running dart migrate using dart included in your Flutter SDK Flutter/bin folder.

Unable to run any flutter commands 'The flutter tool cannot access the file or directory'

Whenever I run a flutter command including:
flutter doctor
flutter clean
flutter channel [any channel]
etc
I get the following error:
Gregorys-MacBook-Pro:~ gregory$ flutter doctor
Unhandled exception:
Exception: Flutter failed to create a directory at "/Users/gregory/.config/flutter". The flutter tool cannot access the file or directory.
Please ensure that the SDK and/or project is installed in a location that has read/write permissions for the current user.
#0 throwToolExit (package:flutter_tools/src/base/common.dart:10:3)
#1 _throwFileSystemException (package:flutter_tools/src/base/error_handling_io.dart:749:3)
#2 _handlePosixException (package:flutter_tools/src/base/error_handling_io.dart:697:3)
#3 _runSync (package:flutter_tools/src/base/error_handling_io.dart:561:7)
#4 ErrorHandlingDirectory.createSync (package:flutter_tools/src/base/error_handling_io.dart:408:12)
#5 new Config (package:flutter_tools/src/base/config.dart:33:17)
#6 runInContext.<anonymous closure> (package:flutter_tools/src/context_runner.dart:146:21)
#7 AppContext._generateIfNecessary.<anonymous closure> (package:flutter_tools/src/base/context.dart:104:41)
#8 _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:311:23)
#9 AppContext._generateIfNecessary (package:flutter_tools/src/base/context.dart:92:20)
#10 AppContext.get (package:flutter_tools/src/base/context.dart:121:32)
#11 config (package:flutter_tools/src/globals.dart:47:30)
#12 getBuildDirectory (package:flutter_tools/src/build_info.dart:673:48)
#13 defaultDepfilePath (package:flutter_tools/src/bundle.dart:29:55)
#14 new BuildBundleCommand (package:flutter_tools/src/commands/build_bundle.dart:24:42)
#15 new BuildCommand (package:flutter_tools/src/commands/build.dart:33:19)
#16 main.<anonymous closure> (package:flutter_tools/executable.dart:102:5)
#17 run.<anonymous closure> (package:flutter_tools/runner.dart:50:13)
<asynchronous suspension>
#18 AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:150:19)
<asynchronous suspension>
#19 AppContext.run (package:flutter_tools/src/base/context.dart:149:12)
<asynchronous suspension>
#20 runInContext (package:flutter_tools/src/context_runner.dart:73:10)
<asynchronous suspension>
#21 main (package:flutter_tools/executable.dart:90:3)
<asynchronous suspension>
Just before I started getting this error I did the following:
Deleted Library/developer/coresimulator/caches folder
Changed flutter channel to beta
I was trying to get disk space for an xcode update.
My problem was importing a project from Windows. I compared the imported folder with one that was created on MacOS and the difference was that appeared extended attribute. So my solution was to remove the extended attributes via command line:
$ sudo xattr -c -r <project_folder>
I also faced same issue when i migrate flutter version to 2.0. Only the solution was grant permission to the user for that location.
you may simply run this command :
$ sudo chown -R <username> /Users/<username>/.config
should be replaced with your user name.
go to flutter sdk folder then,inside bin folder open this path in terminal.now run a command "git stash"
now perform your command "flutter channel beta"
According to the error listed, ensure that the SDK and/or project is installed in a location that has read/write permissions for the current user. Are you sure flutter is not installed in a directory that requires superuser privileges?
I am not sure why changing the channel to beta made the above happen, but what I did to solve it is give myself access to the .config folder, then changed back to stable channel.
I am now encountering different problems by flutter commands are working again
I had the same issue while using vs code in windows, I just ran vs code as administrator and it solved my problem.
helps on MacOS
sudo chown -R $(whoami) /usr/local/Caskroom/flutter
This is what worked for me, if you are on Windows follow these steps;
Go to Windows Security=>Virus & threat protection=>Manage settings=>Controlled folder access=>Allow an app through Controlled folder access .
Click on Add an allowed app
Here you can choose either Recently blocked apps or Browse all apps
Select from the list the apps you want to unblock.
Hope this helps
I am facing this issue for a long time link.
While installing flutter, getting this error. Not able to understand why this is happening every time. This time I try to remove the flutter completely.
After this step, when ever I try to run flutter doctor, that same red line appears.

flutter build apk --release fails while flutter build apk -- debug works fine

I need to release a signed apk for my flutter project, but the "flutter build apk --release" command fails.
The "flutter build apk --debug" command works fine.
I have checked the setup using "flutter doctor" and everything seems fine.
I have tried cleaning the project with flutter clean but it didn't help.
The error returned by the "flutter build apk --debug -v" is:
Gradle build failed to produce an Android package.
#0 throwToolExit (package:flutter_tools/src/base/common.dart:24:3)
#1 _buildGradleProjectV2 (package:flutter_tools/src/android/gradle.dart:484:7)
<asynchronous suspension>
#2 buildGradleProject (package:flutter_tools/src/android/gradle.dart:346:14)
<asynchronous suspension>
#3 buildApk (package:flutter_tools/src/android/apk.dart:34:9)
<asynchronous suspension>
#4 BuildApkCommand.runCommand (package:flutter_tools/src/commands/build_apk.dart:51:11)
<asynchronous suspension>
#5 FlutterCommand.verifyThenRunCommand (package:flutter_tools/src/runner/flutter_command.dart:559:18)
#6 _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:77:64)
#7 _rootRunUnary (dart:async/zone.dart:1132:38)
#8 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#9 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#10 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#11 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#12 Future._complete (dart:async/future_impl.dart:473:7)
#13 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#14 _AsyncAwaitCompleter.complete.<anonymous closure> (dart:async-patch/async_patch.dart:33:20)
#15 _rootRun (dart:async/zone.dart:1124:13)
#16 _CustomZone.run (dart:async/zone.dart:1021:19)
#17 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:947:23)
#18 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#19 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#20 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:115:13)
#21 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:5)
Error was due to invalid password for the keystore. After fixing it, build compleded correctly.
According to Android SDK docs you can use the following command depending on the sdkmanager location: Docs on --licenses option seems to be missing though.
yes | sdkmanager --licenses
GNU/Linux Distributions:
yes | ~/Android/Sdk/tools/bin/sdkmanager --licenses
macOS:
export JAVA_HOME=/Applications/Android\
Studio.app/Contents/jre/jdk/Contents/Home
yes | ~/Library/Android/sdk/tools/bin/sdkmanager --licenses
Windows:
%ANDROID_HOME%/tools/bin/sdkmanager --licenses
Run Flutter Clean Also
If you have following this link properly of Signing the app if you want to upload it to play store then following command should work properly :
flutter build apk --release
If above doesn't work try to use :
flutter build apk // this command default creates the release apk, but don't know if you can upload it to play store