Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Hot Reload to multiple devices

Is it possible to connect multiple devices with hot reload? Specifically, both Android and iOS emulators at the same time.

like image 616
Japang LY Avatar asked Aug 03 '18 09:08

Japang LY


People also ask

How do I run multiple devices on Flutter?

type in your terminal:chmod 755 run_all.sh. There are many ways to do this as previously answered. If you use VS Code instead of Android Studio as your Flutter IDE, this is how you can use the VSC launch configuration and tasks to run concurrently from a single launch and have hot reload enabled for all devices.

What is the difference between hot restart and hot reload?

1. Hot Reload allows us to see the reflected change after bug fixes, building User interfaces and even adding certain features to the app without running your application afresh over and over again. Hot restart destroys the preserved State value and set them to their default.

How do you auto reload on Flutter?

Open Settings. Paste this text > dart. flutterHotReloadOnSave on the settings search box.


2 Answers

This works for me in Android Studio:

  1. Start both emulators / connect devices

  2. Run your code with flutter run -d all

  3. Press r to hot reload

like image 70
Andrey Rankov Avatar answered Oct 05 '22 12:10

Andrey Rankov


If you're using VS Code as your Flutter IDE, this is how you can use the VSC launch configuration and tasks to run concurrently from a single launch and have hot reload enabled for all devices.

If you have an issue with executing flutter run -d all this is an alternative solution will which let you specify the devices that should run. Ensure that the devices you specify are available when running flutter devices.

Your current launch.json file may look something like this:

{     "version": "0.2.0",     "configurations": [         {             "name": "Flutter",             "type": "dart",             "request": "launch",             "flutterMode": "debug"                 }     ] } 

Setup

You will need to update this launch.json file and create tasks.json in the same .vscode folder that is in your application's root directory.

Application folder file structure once the VSC config files are created

Paste only the below code into launch.json

{     "version": "0.2.0",     "configurations": [         {             "name": "Flutter-All",             "preLaunchTask": "Flutter-Launch-All",             "type": "dart",         },         {             "name": "Flutter-iOS",             "preLaunchTask": "Flutter-Launch-iOS",             "type": "dart",         },         {             "name": "Flutter-Android",             "preLaunchTask": "Flutter-Launch-Android",             "type": "dart",         },         {             "name": "Flutter-Web",             "preLaunchTask": "Flutter-Launch-Web",             "type": "dart",         }     ], } 

Paste only the below code into tasks.json

{   "version": "2.0.0",   "tasks": [     {       "label": "Flutter-Launch-All",       "dependsOn": [         "Flutter-Launch-iOS",         "Flutter-Launch-Android",         "Flutter-Launch-Web"       ]     },     {       "label": "Flutter-Launch-iOS",       "type": "shell",       "command": "flutter run -d 'iPhone 11' "     },     {       "label": "Flutter-Launch-Android",       "type": "shell",       "command": "flutter run -d 'AOSP on IA Emulator' "     },     {       "label": "Flutter-Launch-Web",       "type": "shell",       "command": "flutter run -d 'Chrome' "     }   ] } 

Replace the device names accordingly ('iPhone 11', 'AOSP on IA Emulator', 'Chrome').

Firing up all devices

Press the F5 key.

And you're done.

If the F5 shortcut to Start Debugging does not work for you, navigate to Debug & Run on the side panel and select the Flutter-All Configuration you've just created and then Run.

Debug & Run Menu > Configuration Selection

You will then see the terminal window appear and will able to switch between the individual hot-reload sessions running (as Tasks in their own shell).

Individual Terminal Sessions with each Flutter Device running concurrently

Some Background

We use 'Compound Tasks' by way of the dependsOn option on a Task and not 'Compounds' which are for Configurations.

As it is not possible to launch Configurations concurrently, only sequentially, we use Tasks which can run concurrently.

Hence, the "Flutter-All" Configuration executes the Tasks of the iOS, Android and Web Configurations.

If using Compounds, a Configuration will need to complete before the next runs which is not what we want. With Tasks we can choose to execute them sequentially however by default they will execute concurrently when using the dependsOn option.

//Do not use this in your launch.json file unless you want to debug one platform at a time.  "compounds": [     {         "name": "Flutter-All",         "configurations": ["Flutter-iOS", "Flutter-Android", "Flutter-Web"]     } ] 
like image 43
Darshan Kassen Avatar answered Oct 05 '22 11:10

Darshan Kassen