VSCode hot reload for flutter - visual-studio-code

I've just started playing with Flutter in VSCode. I also installed the Dart Plugin.
Running the demo app I read in the terminal
Is this the only way to hot-reload the app? I mean I should always keep the terminal open and focus on it to type "r" in order to reload my views?
Isn't there a shortcut directly from VSCode?

There's an extension for that. Called Dart Code and another one named Flutter Code
They will detect that your project is a Dart/Flutter project. And allows you to debug it + hot reload using f5.

For 2022, this solution worked.
Steps
Open Settings.
Paste this text > dart.flutterHotReloadOnSave on the settings search box.
And change value from "Manual" to "Always"

If you like to hot reload your app with a keybinding better than Ctrl+Shift+F5, just change the Debug: Restart to Ctrl+S, so whenever you trigger the the Ctrl+S the app will first save your changes according to the workbench.action.files.save and afterwards restart the app (=hot reload, it is the green circle you see in the debugbar).
Keybindings for VS Code:

VSCode debug -> start debuging, make a change and try, That's what you want.

No idea why F5 does not hot reload for me :(
But, you can map the -
Flutter: Hot Reload
command to whatever key combo floats your boat via -
Preferences > Keyboard ShortCuts
as in this screenshot -

open the Debug sidebar from VSCode and use it
then when you save, it will hot reload and apply the changes you make
that is what working with me

use the green reload button to hot reload the app in VS Code

on Mac select
Run without Debugging or Shift+f5
you will see this at top select the electric icon for Hot Reload.
After doing some changes save it. It will auto reflect changes.

yes Here is the plugin Dart Code for VS CODE
https://marketplace.visualstudio.com/items?itemName=Dart-Code.dart-code
here is official doc for VsCode flutter
https://flutter.io/get-started/editor/#vscode

In VS once installed Dart + Flutter extension, 2 options to hot reload 1) Use combo keys Command + Shift + F5. 2) Save the file by use combo keys Command + S

I have both extensions and VSCode is set to Hot Reload once a document is saved. The only time I can reload my app is by stopping and restarting my debugger

i dont know why but my vscode hot reloading not work if you have this problem you can use ctrl + f5 to refresh app.
hope to work for developers that hot reloading not work for them.

You need to run Flutter app from VS Code's built-in debugger not VS Code's terminal.

Select ▷ then "Run without Debugging" -> You can now use ⌘S on Mac to ⚡ Hot Reload ⚡

===== Aug 2022 UPDATE =====
v3.42 and above: You can enable hot reload on autosave in the latest version by setting Flutter Hot Reload On Save to allIfDirty in your VSCode settings.
===== Old Versions (Early 2022 and below) =====
v3.41 and below: You can enable hot reload on autosave in the latest version by setting Flutter Hot Reload On Save to always in your VSCode settings.
v3.19 only: They disabled hot reload on autosave completely.
v3.18 and below: The extension hot reloads automatically when auto saving.

if flutter and dart extensions already installed on vscode
Set from manual to All

If you run your flutter project from the terminal you will have to manually press r to do a hot reload.
Therefore use the Run and Debug option in the VS Code to test your project.

You may encounter problems with Hot Reload due to VS Code issues like this, which is relevant while writing this answer.
The most convincing way I see to do it without downgrading, updating to Insiders builds and/or loosing dev speed is to use the green restart/reload button in the VS Code run widget or Ctrl+Shift+F5 shortcut, but this option may be inacceptable for projects with trickier navigation and few different views.

Related

Flutter hot reload doesn't work on autosave in VSCode

Flutter hot reloads every time VS Code auto-saves my project before. A few hours ago, I started experiencing this issue where auto-saving doesn't hot-reload my app. Pressing Ctrl+S manually triggers hot reload but auto-saving does not. I've tried re-installing VS Code and Flutter SDK but the problem still exists.
My newly installed VS Code only has the following extensions and settings:
Dart: v3.19.1
Flutter: v3.19.0
Auto-save is set to afterDelay of 1000ms
How do I hot reload when autosaving in vscode? Another person had the same issue two hours ago: https://github.com/Dart-Code/Dart-Code/issues/3110
An update in Dart & Flutter's VS Code extension is causing the issue.
===== Latest Version =====
3.42 and above: You can enable hot reload on autosave in the latest version by setting Flutter Hot Reload On Save to allIfDirty in your VSCode settings.
===== Old Versions =====
v3.41 and below: You can enable hot reload on autosave in the latest version by setting Flutter Hot Reload On Save to always in your VSCode settings.
v3.19: They disabled hot reload on autosave completely.
v3.18 and below: The extension hot reloads automatically when auto saving.
It's in the change notes for this release. It's an intended feature! (http://dartcode.org/releases/v3-19)
It says:
#3033: Hot-reload-on-save no longer fires for Save events triggered by VS Code’s auto-save feature. Pressing Save manually will still trigger a reload even if there are no unsaved changes.
Doesn't anybody read the release notes? :)
Just delete your VS cache folder and VS temp folder.
-> Press windows button and type:
%temp%
%appdata%
-> Check your launch.json in .vscode folder. delete and recreate if necessary.

How to disable auto hot reload on flutter?

I have search for this issue and seen solutions, but I am not able to figure out the issue.
i get some error because every time write the code, its directly run hot reloads before i finish complete the code. so, how to disable automatic hot reloads in flutter?
automatic hot reloads in flutter is so annoying, i just want to use hot reloads base on Ctrl + S. how i do that?
i'am using visual studio code by the way.
If you are using Android Studio 4.1 go to:
On Mac: Android Studio -> Preferences -> Languages & Frameworks -> Flutter
On Windows: File -> Settings -> Languages & Frameworks -> Flutter
Then, after being at Flutter section, look for "App Execution" and there you have to uncheck the box "Perform hot reload on save". Then click OK and that's it.
Here is how:
Disable auto save on VsCode:
Go to File > Preferences > settings
Choose off from the auto save drop down.
In VS Code, alternative to turning off auto-save:
Turn off these settings:
Settings > Extensions > Dart & Flutter > Flutter Hot Reload On Save
Settings > Extensions > Dart & Flutter > Flutter Hot Restart On Save
Then use Run > Restart Debugging to restart manually or Crtl + F5 to Hot Reload
To disable Auto Save on VS Code
Go to File > Preferences > settings turn off Auto Save
To disable Hot Reload or Hot Restart on save search setting with auto hot, and disable the setting.
If you are using Intellij IDEA (or Android Studio) you might as well disable Hot UI if enabled:
File > Preferences > Languages & Frameworks > Flutter > Enable Hot UI (disable it)

Can not change or remove logs from project

I'm facing a weird issue. I inserted some logs just like below:
log('whatever messager you want');
Now, no matter if I comment them or change the message inside them, nothing changes. Thay remain there the way I set it the first time. It happens in both VSCode and Android Studio.
I already tried:
deleting and installing the app again.
reload vscode window
close and reopen vscode
restart windows
Nothing worked for me.
Have you tried to run
flutter clean
in your project directory?
In my case, Ctrl + Shift + B was displaying No build task to run found and ctrl + F5 wasn't updating my code changes when using VSCode.
The solution I found was to migrate to Android Studio. I relaunched the emulator from Android Studio and the Run button updates the app correctly.

VS Code and Flutter/Dart, Widget Autocomplete not working

I'm working with some tutorials to learn Flutter and the one that I'm taking now uses VSCode. I actually prefer it to Android Studio because it doesn't overheat my Macbook. The only frustration I've had with it so far is that there are quite a few times where autocomplete just stops working. For instance, if I wrap a Column in a widget and then start typing Container, Android Studio will pop up an autocomplete showing Container VS Code used to do this for me, but has stopped for some reason. I can still get the autocomplete list if I press Ctrl+Spacebar.
I'm using the following:
VS Code - 1.36.1
Dart Extension - 3.2.0
Flutter Extension - 3.2.0
Material Icon Theme - 3.8.0
This is the latest version for all of these.
Is there some setting or something that I need to change? I've spent the last two hours trying to work out what's going on. In searching, I did see some references to earlier versions of VS Code and the extensions causing problems, but nothing about these versions.
Go to pubspec.yaml and hit save or run get packages. It worked for me, hope it helps
There are generally two reasons for VS Code to automatically open code completion - 1) when you press a key that the extension ahs told VS Code should invoke it and 2) when VS Code thinks it would be useful (for example when you start typing on a blank line). If you're typing in a line of existing code it won't always do this.
Pressing Ctrl+Space is the correct way to open completion in the cases where it doesn't appear automatically.
That said, if you can provide more information about a specific instance, I can take a look if it's something we could/should pop completion open for. It would be best raised at https://github.com/Dart-Code/Dart-Code/issues with a screenshot and details of exactly where your cursor is (and how you got there).
dont use too many flutter extension, I also face this problem , then I realize that 3 flutter extension are installed, use only one , it works for me, try it.
I did have a derivative of this problem when I was working with VS Code and flutter and this is what I did.
This is for windows though. This is what I did ;
Go to C:\Users<your username >\AppData\Roaming\Code and delete all the files and the directory called "Code". Usually the folder AppData is hidden so you'll have to unhide it. Then restart your computer and then reopen VS Code. This I think cleared some of the unwanted cache files which kind of made things right again.
Hope it helped.
Make sure
Preferences > Editor > Intentions > Quick assist powered by the Dart
Analysis Server
is enabled (Screenshot).
If so, most likely your Dart Analysis server is crashing. Navigate to your HOME folder:
cd ~
And then below should resolve your issue:
sudo chown -R $USER .dart .flutter
Clear your cache if the issue still exists:
File > Invalidate Caches > Clear file system cache and Local History > Invalidate and Restart.
Also, Power Save Mode should be disabled:
File > Power Save Mode
Doing a flutter clean and then flutter pub upgrade worked for me. Sometimes flutter clean using the command line does not work if you have android studio then in tools go to flutter and then do flutter clean.
just do flutter clean then flutter create. and then flutter pub upgrade, and also see if your flutter SDK is latest or not.
Check to make sure Android Studio is not on low power mode
Menu>File>Low Power Save Mode
I solved in in my Macbook Pro by
Going to System Preferences > Keyboard > Shortcuts > Input Sources
Disable: Select the previous input source (^ space)
Updating VS Code dart and flutter extensions
Restarting the computer.
And voila! Ctrl + space now shows options in VS Code.

Hot Restart (previously Full restart) in Flutter VS Code whilst debugging

Whilst running my app using flutter run in the console I can press 'r' to do a reload and press 'R' to do a reload and restart. Is there a way to do this whilst debugging in VS Code? Saving a file causes a hot reload but I can't figure out a way to do a full restart.
Edit
This is a little bit different to the hot reload option so I don't think this is a duplicate. This question regards performing a full restart of the application without reinstalling it. This is possible by pressing a capital R in the console, as opposed to pressing a lowercase r in the console which would perform a hot reload.
When debugging you can press Ctrl+F5 for a full restart, or you can run the Flutter: Full Restart command from the VS Code's command palette (Ctrl+Shift+P or Cmd+Shift+P for macOS).
Note: In the next version (v2.13.0, which there's a beta of available) "Full Restart" has been renamed to "Hot Restart" to better reflect how it works (this is happening across the other Flutter tools and docs too).
Related info:
Flutter VS Code docs
Dart Code keybindings
Dart Code debugging commands
The chosen answer wasn't working for me with Flutter 1.2.1 so I used this solution:
get the Flutter extension
search for dart.flutterDebuggerRestartBehaviour in VS Code settings and set it to hotRestart
start the debugger (F5) and click the restart button when you want to hot restart: