Jitesh Mohite
Jitesh Mohite

Reputation: 34180

Flutter Hot reloading not working in android studio(mac)

While working on mac machine I found that hot reloading not working in Android Studio. I don't know what's wrong :)

Here is the code which not reloading(I am changing the Hello world text to Flutter Sample text)

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new Scaffold(
      appBar: new AppBar(
        title: new Text("Demo Project"),
      ),
      body: Center(child: new Text("Hello World!!!")),
    ),
  ));
}

Please see this below video with the link https://www.dropbox.com/s/e7viujcgv8w0mtr/hot_reload.mp4?dl=0

This not stop my development, But I am concerned why it is happening.

Upvotes: 39

Views: 51738

Answers (26)

Trịnh Đức Duy
Trịnh Đức Duy

Reputation: 253

it work for me in Android Studio Koala. Please remove shortcut for Manual Live Edit in Settings => Keymap => search "Manual Live Edit"

enter image description here

Upvotes: 0

Lukas Pierce
Lukas Pierce

Reputation: 1259

I constantly have similar problems when there are new unapplied Mac OS updates. You can disable auto-updates or install updates from incoming notifications.

mac os updates

Upvotes: -1

Hashim Jacobs
Hashim Jacobs

Reputation: 96

My issue was that I was running flutter run from the Android Studio terminal instead of clicking the Play or Debug button to run my application.

Upvotes: 1

Mohammad Hossein Amri
Mohammad Hossein Amri

Reputation: 1985

A hot reload in android studio against vscode which is Ctrl+S is triggered by Save All or explicitly calling the hot reload

enter image description here https://docs.flutter.dev/get-started/test-drive?tab=androidstudio

A workaround is to set Ctrl+S as the save all shortcut. Out of the box it's Ctrl+K, S, that's why you see other solution that is suggesting to reset your keymap to Intellij

enter image description here

Upvotes: 4

wantyouring
wantyouring

Reputation: 225

In my case, when I used the VSCode keymap plugin, the hot reloading on save did`nt work. After I changed the keymap to IntelliJ keymap, the hot reloading on save worked properly.

enter image description here

Upvotes: 3

James
James

Reputation: 679

In my case, hot reload failed due to insufficient storage space on the emulator. I didn't get an error in the console until I stopped and tried to re-run the project. To get hot reload working in my case, I had to stop the emulator, and increase the storage capacity on the device, and re-run the project.

Upvotes: 0

Affan Minhas
Affan Minhas

Reputation: 567

Hot reload works only out of the main method. If your is inside the main method try to make a class of stateless or stateful widgets and pass the name as a parameter to runApp()

Upvotes: 0

Piyush
Piyush

Reputation: 813

In my case, it was just extracting the whole MaterialApp into it's own stateless Widget.

Before -

void main() {
  runApp(MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.teal,
        body: SafeArea(
            child: Container(
          color: Colors.white,
          child: Text("Helloo"),
          height: 100,
          width: 100,
          margin: EdgeInsets.all(20),
        )),
      ),
    ));
}

After -

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.teal,
        body: SafeArea(
            child: Container(
          color: Colors.white,
          child: Text("Helloo"),
          height: 100,
          width: 100,
          margin: EdgeInsets.all(20),
        )),
      ),
    );
  }
}

Even VS Code complains if you are just defining your whole app inside main function.

Solved! Enjoy :)

Upvotes: 7

JoKr
JoKr

Reputation: 5256

Hot reload stopped working suddenly in my project. Using Android Studio and Windows.

When moving files around, the Android Studio would refactor imports, which is neat. But it would use absolute imports (C://Users/Projects/.../file.dart) and that would break hot reload completely. Be careful when moving files.

Upvotes: 2

Shamim Shaikh
Shamim Shaikh

Reputation: 827

There are following steps to resovle this issue

  1. Check the import package line it should be like packages: your file not file:///

  2. use command flutter clean then flutter pub get in your terminal / command promp

NOTE: you have to use these command from your project root directory

Its work for me

Upvotes: 0

Alexander
Alexander

Reputation: 646

Encountered same issue with Flutter 1.22.6. But worked fine when I used Cmd+S instead of Hot Reload (lightning) button

Upvotes: 0

Akbar Pulatov
Akbar Pulatov

Reputation: 3319

In my case i just deleted the old app from phone. Then run from Android Studio. Maybe i tried release app and don't delete after(can't remember exactly). In any case, it is solved!

Upvotes: 0

Raj A
Raj A

Reputation: 592

These might cause because of too many codes and assets. Simply delete the app from the emulator and rerun it again.

Upvotes: 0

vishwajit76
vishwajit76

Reputation: 2448

Try This

The solution on GitHub Reloaded 0 of NNN libraries in Xms

Android Studio uses IDEA to import files automatically, Like this

import 'file///F:/flutter_project/lib/home_page.dart';

Change it to

import 'package:flutter_project/home_page.dart';

Upvotes: 22

Sharad Meghnathi
Sharad Meghnathi

Reputation: 29

Put your code inside stateless of stateful widget. To make HOT RELOAD work code must be inside stateless of stateful widget.

Upvotes: 2

Abhas Bhoi
Abhas Bhoi

Reputation: 387

I am using VScode for flutter in Windows. Was facing same issue with hot reload. Updated Dart and Flutter package to latest version and luckily it started working.

Upvotes: 1

Raghunath
Raghunath

Reputation: 171

  1. Goto android studio and click help and select check for updates
  2. Now click on update button.
  3. After restart goto plugins and reinstall the flutter plugin by uninstalling and reinstalling the pugins.

Upvotes: 0

Invin
Invin

Reputation: 962

Flutter Documentation says "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()"

So, it looks like if the app logic resides outside the main method, then Hot Reloading works. Try to refactor your app like below and then do Restart (green button). Any subsequent changes you make, will be HOT RELOADED when saved.

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text("Demo Project"),
        ),
        body: Center(child: new Text("Hello World!!!")),
      ),
    );
  }
}

UPDATE: Hot Reload will not work if you are adding new assets (like images, or medial files). In those cases you will have to restart the App.

Upvotes: 76

LP Square
LP Square

Reputation: 1135

Wrap your code with a StatefulWidget like shown in the code below. This might be confusing when watching old tutorials because it used to work without it on an earlier Flutter Version.

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Demo Project"),
      ),
      body: Center(child: new Text("Hello World!!!")),
    );
  }
}

Upvotes: 6

Russo
Russo

Reputation: 2982

How to use Flutter Hot Reload in a StatelessWidget inside Android Studio 3.6.2 and Flutter v1.12.13+hotfix.9 on Linux ... inside main.dart file:

Make sure in Android Studio > File > settings > Languages & Frameworks > Flutter > check "Perform hot reload on save"!!!

Use the code below -> change the 'Hello' -> save it -> see the hot reload result

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // StatelessWidget turns this class into a widget
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Text('Hello'),
    );
  }
}

Upvotes: 1

Mad Dog
Mad Dog

Reputation: 21

You have to save your changes you made in main.dart file. After that hot reload will work, on visual studio select auto-save option then it will work perfectly fine.

*Hot reload needs to save your files first.

Upvotes: 0

Robert Stevens
Robert Stevens

Reputation: 549

This works if all else fails: 1. Close Android Studio and the Simulators

  1. Delete Android Studio by opening the Finder, click Applications, right click on Android Studio and click Move to Trash

  2. Remove Android files: $ cd ~/Library/Android $ rm -fr * $ cd $ rm -fr .android

  3. Remove Java https://www.java.com/en/download/help/mac_uninstall_java.xml

  4. Install Java

  5. Install and Launch Android Studio and accept the license agreements https://developer.android.com/studio/install

Upvotes: 0

Luiz Fran&#231;a
Luiz Fran&#231;a

Reputation: 106

If I'm not mistaken, hot reload calls the build method of your widget, as the change you made weren't made inside a build method, it didn't change anything in the screen.

Try changing the texting inside the MyWidget Build method and see if it works.

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new Scaffold(
      appBar: new AppBar(
        title: new Text("Demo Project"),
      ),
      body: new MyWidget(),
    ),
  ));
}

class MyWidget extends StatelessWidget {
  const MyWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(child: new Text("Hello World!!!"));
  }
}

Upvotes: 5

Manoj Perumarath
Manoj Perumarath

Reputation: 10204

This is what you're looking for

enter image description here In File -> Settings -> Keymap, you should assign some key shortcut for that to happen. Default it will be Ctrl+\

Upvotes: -5

Xihuny
Xihuny

Reputation: 1538

in the terminal, run

flutter doctor -v

flutter upgrade

Also, try File > Invalidate Caches / Restart... from Android studio menu

Upvotes: 5

Abdoulaye BARRY
Abdoulaye BARRY

Reputation: 807

I had the same problem. To solve it you need to update flutter sdk.

Upvotes: 0

Related Questions