[FIXED] Flutter Hot reloading not working in android studio(mac)

Issue

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.

Solution

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.

Answered By – Invin

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published