Getting error in hot reload and showing exception - flutter

I am getting following error on hot reload:
Dart Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4041 pos 12: '!_debugLocked': is not true.
Here is log:
[VERBOSE-2:shell.cc(210)] Dart Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4041 pos 12: '!_debugLocked': is not true., stack trace: #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
#1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2 NavigatorState.build (package:flutter/src/widgets/navigator.dart:4041:12)
#3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
#4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4627:15)
#5 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11)
#6 Element.rebuild (package:flutter/src/widgets/framework.dart:4343:5)
#7 StatefulElement.update (package:flutter/src/widgets/framework.dart:4832:5)
#8 Element.updateChild (package:flutter/src/widgets/framework.dart:3314:15)
#9 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
#<…>
Reloaded 46 of 1237 libraries in 575ms.

It looks like a problem with internal state management. In this case, the simplest solution is to hot-restart the app. Notice the difference between hot reload and hot restart: (https://flutter.dev/docs/development/tools/hot-reload)
What is the difference between hot reload, hot restart, and full restart?
Hot reload loads code changes into the VM and re-builds the widget tree, preserving the app state; it doesn’t rerun main() or initState(). (⌘\ in Intellij and Android Studio, ⌃F5 in VSCode)
Hot restart loads code changes into the VM, and restarts the Flutter app, losing the app state. (⇧⌘\ in IntelliJ and Android Studio, ⇧⌘F5 in VSCode)
Full restart restarts the iOS, Android, or web app. This takes longer because it also recompiles the Java / Kotlin / ObjC / Swift code. On the web, it also restarts the Dart Development Compiler. There is no specific keyboard shortcut for this; you need to stop and start the run configuration.

Related

Flutter: Hot reload and hot restart not working after connecting to firebase background functionality

Flutter hot reload and hot restart stop working after doing the minimum to connect my app to a firebase project.
I have connected three different flutter projects to three different firebase projects and none of them have allowed me to hot reload or hot refresh. After following the instructions on firebase to connect my app the only thing I add is this.
main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
and I always get the same errors:
Unhandled exception:
Null check operator used on a null value
#0 IncrementalCompiler._initializeExperimentalInvalidation (package:front_end/src/fasta/incremental_compiler.dart:1226:53)
<asynchronous suspension>
#1 IncrementalCompiler.computeDelta.<anonymous closure> (package:front_end/src/fasta/incremental_compiler.dart:324:11)
<asynchronous suspension>
#2 IncrementalCompiler.compile (package:vm/incremental_compiler.dart:68:50)
<asynchronous suspension>
#3 FrontendCompiler.compile (package:frontend_server/frontend_server.dart:572:11)
<asynchronous suspension>
#4 listenAndCompile.<anonymous closure> (package:frontend_server/frontend_server.dart:1210:11)
<asynchronous suspension>
This is not Flutter's Hot reload and hot restart issue. This may occur due to the following:
Installed packages (pub).
Nullability
You should check the compatibility of some of your packages with the firebase package. Your log shows frontend_server.dart and incremental_compiler.dart contain null checks on null values.
If you are the creator of those files, you need to update the codebase by handling null this way:
if (value != null) {
// do something
}
Else
flutter pub clean cache
flutter pub get
flutter build

Flutter error "dart_vm_initializer.cc" Unhandled Exception: 'dart:ui/platform_dispatcher.dart'

I had to reinstall Flutter, Android Studio, Sdk, recreate phone emulator, recreate all my Flutter project because time passes and problems and framework bugs pile up. To keep updated all the Flutter environment without problems seems to be impossible. All the things becomes unstable when new and new Flutter and/or Sdk versions come out.
So I reinstalled everything to have a new, perfectly working environment.
Well, now I have a new, never seen, error:
E/flutter ( 3692): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'dart:ui/platform_dispatcher.dart': Failed assertion: line 1840 pos 15: '<optimized out>': is not true.
E/flutter ( 3692): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
E/flutter ( 3692): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
E/flutter ( 3692): #2 new Locale.fromSubtags (dart:ui/platform_dispatcher.dart:1840:15)
E/flutter ( 3692): #3 PlatformDispatcher._updateLocales (dart:ui/platform_dispatcher.dart:762:29)
E/flutter ( 3692): #4 _updateLocales (dart:ui/hooks.dart:64:31)
E/flutter ( 3692):
My app works, but how can I get rid of that bug?

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.

MissingPluginException(No implementation found for method pickImage on channel plugins.flutter.io/image_picker)

Unhandled Exception: MissingPluginException(No implementation found for method pickImage on channel plugins.flutter.io/image_picker)
E/flutter (16028): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:7)
E/flutter (16028): <asynchronous suspension>
E/flutter (16028): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
E/flutter (16028): #2 MethodChannelImagePicker.pickImagePath (package:image_picker_platform_interface/src/method_channel/method_channel_image_picker.dart:62:21)
E/flutter (16028): #3 MethodChannelImagePicker.pickImage (package:image_picker_platform_interface/src/method_channel/method_channel_image_picker.dart:30:25)
E/flutter (16028): #4 ImagePicker.getImage (package:image_picker/image_picker.dart:101:21)
STEP 1 : Ensure you have imported the plugin by including this in your pubspec.yaml file
image_picker: ^0.6.7+4
STEP 2 : Make sure you have imported the plugin in the file you need it
import 'package:image_picker/image_picker.dart';
STEP 3 : Make sure you run this command
flutter pub get
Now, many times in Flutter even after doing the above 3 steps perfectly you will get the same error.
Here is my solution that has worked for me.
STEP 1 : Run this command in the terminal
flutter clean
STEP 2 : CLOSE/STOP your emulator and start it again
99% of the time this works for me.
just click stop red icon right side of run icon. and restart app