Friday 21 October 2022

Flutter Dart Error: Can't load Kernel binary - Invalid kernel binary

During working with Flutter, Error "Can't load Kernel binary" may arise unexpectedly after we make some changes to the application. This error is often confusing for developers because it often does not specify a clear cause. Even if you haven't done anything wrong, mistakes can still appear spontaneously. With luck, cleaning the project can fix this problem, but in many cases it doesn't work.

Example of an error message:

[ERROR:flutter/shell/common/] Dart Error: Can't load Kernel binary: Invalid kernel binary: Indicated size is invalid.
   .../runtime/] Could not create root isolate
   ./android/] Could not launch engine in configuration.


Android Studio's compiler sometimes doesn't work as expected, so the process of linking libraries to compile Dart source code may encounter errors that distort the project's configuration paths. This results in errors during or after compilation with cryptic errors, even errors reported that seem absurd, unrelated to the problem. (It appears to be similar to the bug in Android Studio's Dart code suggestion function. In many cases Android Studio suggests inserting const or required keywords but inserts these keywords in the wrong place. which it is suggested.).
Once errors of this type arise, it is often cached in the project, so many times we try to find ways to not handle the error, but sometimes it suddenly disappears and cannot be re-appeared.

"Dart Error: Can't load Kernel binary: Invalid kernel binary: Indicated size is invalid."

Solve it

Please try the following steps one by one until the problem is resolved

Step 1: Clear project cache
Completely close the application in the virtual machine.
In the main project directory, run the command: "flutter clean"
Then run "flutter pub get" again to update the libraries. 
Rebuild the project to see if the error has been resolved, You can also try to change a little bit in the application's code to make sure the application has been rebuilt and not loaded from the cache. If the problem is not resolved, try step 2

Step 2: Clear Flutter SDK Cache
Go and delete all files in the cache folder of Flutter SDK.
As required when installing, the Flutter SDK is usually installed in the C drive. For example: C:/flutter
You can also easily find the path to the Flutter SDK in the ".flutter-plugins" file inside your project.
After clearing the cache, run the "flutter doctor" command again to let flutter update the libraries (you can run "flutter doctor" from anywhere).
Re-open Project to see if the problem has been resolved. If not, continue with step 3.

Step 3: Rollback of previous project changes
Try reversing some of the last changes you made to the project's source code. (Remember to save those changes because you can still use them normally if they're not wrong.) Reversing changes, creating changes in the project's source code can help clear caches and miscompiled configuration fragments.
If the error is still not resolved when you have reversed the small segments then try some higher level change in the file "main.dart"

Note: In the process of fixing errors, you should not use "hot restart"

If there is anything more to share in this situation, please leave information in the comment section so we can discuss.