LateInitializationError: Field 'annotationAst' has not been initialized - flutter

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)

Related

Whats the difference between dart run vs flutter pub run

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)

Flutter share_plus is not working and getting error [duplicate]

I am trying to use the plugin
_launchURL(url) async {
await launch("www.google.com");
}
I have put "www.google.com" just for debugging purposes.
The error I get is MissingPluginException :
E/flutter ( 8299): MissingPluginException(No implementation found for method launch on channel plugins.flutter.io/url_launcher)
E/flutter ( 8299): #0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:278:7)
E/flutter ( 8299): <asynchronous suspension>
E/flutter ( 8299): #1 launch (package:url_launcher/url_launcher.dart:47:19)
E/flutter ( 8299): #2 _launchURL (file:///Users/matejsimunic/Work/dart/suhail/lib/main.dart:834:9)
E/flutter ( 8299): <asynchronous suspension>
E/flutter ( 8299): #3 _TripDetailBodyState.build.<anonymous closure> (file:///Users/matejsimunic/Work/dart/suhail/lib/main.dart:818:19)
E/flutter ( 8299): #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
E/flutter ( 8299): #5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:549:30)
E/flutter ( 8299): #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 8299): #7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter ( 8299): #8 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
E/flutter ( 8299): #9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter ( 8299): #10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter ( 8299): #11 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter ( 8299): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter ( 8299): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter ( 8299): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 8299): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter ( 8299): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter ( 8299): #17 _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter ( 8299): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
restarting the app from ide doesn't work.
If you are using hot restart or hot reload, it won't do the trick.
Since Flutter has to inject plugin dependencies into the platform-specific parts of your app, hot restart/hot reload is not enough to trigger the injection.
Check this issue for more.
Close the app and execute flutter run command.
flutter clean
Inside the console did the job as stated here
I faced this Error when using Url_launcher package. just had to stop and run the app, issue got solved
Simply Run these commands..
either--
Flutter clean
or
Invalidate Caches/Restart
Had problem while using flutter toast dependency and dependencies like facebook_login. I followed these steps and resolved it:
First, u need to test whether these toast and shared preference dependencies are causing problems or some other dependencies cause the problem.
For testing that, u need to create a new seprate flutter project having the same flutter toast and shared preference dependency and check now if u r able to get toasts or not.
If u r getting toast so there is no problem with ur toast dependency.
The problem might be in ur one of the social media dependency.
In my case, I followed the above steps and found that problem was in facebook dependency. Finally, I found that I did not provide details of my app at facebook developer console. So implemented that and my app was fine.
In my case it was for the cached_network_image plugin , I have removed cached_network_image from pubspec.yaml and replaced the implementation with Image.network and the problem has disappeared
In my case, I had this problem with the image picker, then I followed the documentation and used an emulator with api 29+, it didn't work before because I was using an emulator with api 28 https://pub.dev/packages/image_picker
Uninstalling and installing the app works for me
just open terminal and
step1 flutter clean
step2 flutter pub get
step3 flutter run
Its worked for me,
Thanks have a good day .
i think if you have already done flutter pub get on the dependency, and added all the requirements for the given platform you just have to flutter run your app

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.

Flutter project compile error after upgrading to 2.0.0

I was using Flutter version 1.25 (beta channel) and it worked just fine. After upgrading to version 2.0.0 and switching to stable channel i'm getting this error when i try to run the app either on mobile or web.
../../../AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/delegate_widget.dart:194:18: Error: Superclass has no method named 'inheritFromElement'.
return super.inheritFromElement(ancestor, aspect: aspect);
^^^^^^^^^^^^^^^^^^
../../../AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/provider.dart:259:19: Error: The method 'inheritFromWidgetOfExactType' isn't defined for the class 'BuildContext'.
- 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'inheritFromWidgetOfExactType'.
? context.inheritFromWidgetOfExactType(type) as InheritedProvider<T>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
../../../AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/provider-3.2.0/lib/src/provider.dart:260:19: Error: The method 'ancestorInheritedElementForWidgetOfExactType' isn't defined for the class 'BuildContext'.
- 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('/C:/flutter/packages/flutter/lib/src/widgets/framework.dart').
Try correcting the name to the name of an existing method, or defining a method named 'ancestorInheritedElementForWidgetOfExactType'.
: context.ancestorInheritedElementForWidgetOfExactType(type)?.widget
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unhandled exception:
NoSuchMethodError: The getter 'name' was called on null.
Receiver: null
Tried calling: name
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 ProgramCompiler._emitSuperTarget (package:dev_compiler/src/kernel/compiler.dart:5109:41)
#2 ProgramCompiler.visitSuperMethodInvocation (package:dev_compiler/src/kernel/compiler.dart:5102:24)
#3 SuperMethodInvocation.accept (package:kernel/ast.dart:5109:44)
#4 ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:3623:20)
#5 ProgramCompiler.visitAsExpression (package:dev_compiler/src/kernel/compiler.dart:5677:18)
#6 AsExpression.accept (package:kernel/ast.dart:6062:44)
#7 ProgramCompiler._visitExpression (package:dev_compiler/src/kernel/compiler.dart:3623:20)
#8 ProgramCompiler.visitReturnStatement (package:dev_compiler/src/kernel/compiler.dart:4152:38)
#9 ReturnStatement.accept (package:kernel/ast.dart:7786:43)
#10 ProgramCompiler._visitStatement (package:dev_compiler/src/kernel/compiler.dart:3551:20)
#11 MappedListIterable.elementAt (dart:_internal/iterable.dart:411:31)
#12 ListIterator.moveNext (dart:_internal/iterable.dart:340:26)
#13 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
#14 new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#15 new List.of (dart:core-patch/array_patch.dart:50:28)
#16 ListIterable.toList (dart:_internal/iterable.dart:211:44)
#17 ProgramCompiler.visitBlock (package:dev_compiler/src/kernel/compiler.dart:3717:62)
#18 Block.accept (package:kernel/ast.dart:7048:43)
#19 ProgramCompiler._visitStatement (package:dev_compiler/src/kernel/compiler.dart:3551:20)
#20 ProgramCompiler._emitFunctionScopedBody (package:dev_compiler/src/kernel/compiler.dart:3562:18)
#21 ProgramCompiler._emitSyncFunctionBody.<anonymous closure> (package:dev_compiler/src/kernel/compiler.dart:3355:17)
#22 ProgramCompiler._withLetScope (package:dev_compiler/src/kernel/compiler.dart:2235:25)
#23 ProgramCompiler._withCurrentFunction (package:dev_compiler/src/kernel/compiler.dart:3389:18)
#24 ProgramCompiler._emitSyncFunctionBody (package:dev_compiler/src/kernel/compiler.dart:3351:17)
#25 ProgramCompiler._emitFunction (package:dev_compiler/src/kernel/compiler.dart:3165:11)
#26 ProgramCompiler._emitMethodDeclaration (package:dev_compiler/src/kernel/compiler.dart:1843:12)
#27 ProgramCompiler._emitClassMethods (package:dev_compiler/src/kernel/compiler.dart:1784:23)
#28 ProgramCompiler._emitClassDeclaration (package:dev_compiler/src/kernel/compiler.dart:658:21)
#29 ProgramCompiler._emitClass (package:dev_compiler/src/kernel/compiler.dart:580:21)
#30 List.forEach (dart:core-patch/growable_array.dart:403:8)
#31 ProgramCompiler._emitLibrary (package:dev_compiler/src/kernel/compiler.dart:529:23)
#32 List.forEach (dart:core-patch/growable_array.dart:403:8)
#33 ProgramCompiler.emitModule (package:dev_compiler/src/kernel/compiler.dart:394:15)
#34 JavaScriptBundler.compile (package:frontend_server/src/javascript_bundle.dart:144:33)
<asynchronous suspension>
#35 FrontendCompiler.writeJavascriptBundle (package:frontend_server/frontend_server.dart:632:5)
<asynchronous suspension>
#36 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:545:9)
<asynchronous suspension>
#37 listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1105:11)
<asynchronous suspension>
Finished with error: the Dart compiler exited unexpectedly.
Failed to compile application.
I tried (from Android Studio) Tools > Flutter > Flutter Clean and File > Invalidate Caches / Restart... but it didn't help.
From the dart.dev site.
The dart fix command (added in Dart 2.12)
finds and fixes two types of issues:
Analysis issues that have associated automated fixes
(sometimes called quick-fixes or code actions)
Issues that have associated package API migration information
To preview proposed changes, use the --dry-run flag:
$ dart fix --dry-run
To apply the proposed changes, use the --apply flag:
$ dart fix --apply
From Flutter 1.X to Flutter 2.X you will see breaking changes. Always review these changes before committing to the upgrade so you understand the changes.
The package provider you're using is on version 3.2.0. It's likely that this package version is using incompatable methods, objects, etc with Flutter 2.0.0.
The latest version of provider is 5.0.0 which supports this new version of Flutter. Try upgrading the package to this version.

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