Eugene Barsky
Eugene Barsky

Reputation: 5992

Hot reload vs hot restart in Android Studio with Flutter

I'm testing my Flutter starter app on virtual as well as physical device. The problem is that the app doesn't update on the screen while hot-reloading (this is configured to work at every file-save), but only on hot-restart. On the screenshot this corresponds to the right button, not the left.

Is it normal or is something wrong?

enter image description here

Just in case, this is the contents of my main.dart file:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          title: Center(
            child: Text('Hi Everybody!'),
          ),
          backgroundColor: Colors.blueGrey[900],
        ),
        body: Center(
          child: Image(
            image: AssetImage(
              'images/diamond.png',
            ),
          ),
        ),
      ),
    ),
  );
}

Upvotes: 0

Views: 775

Answers (1)

MSARKrish
MSARKrish

Reputation: 4094

You are writing everything inside main function. so hot reload is not working.

They mentioned this in Flutter Documentaion

As a general rule, if the modified code is downstream of the root widget’s build method, then hot reload behaves as expected. However, if the modified code won’t be re-executed as a result of rebuilding the widget tree, then you won’t see its effects after hot reload.

so you need to write your code below root widget.

Write code like this:

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.blueGrey,
        appBar: AppBar(
          title: Center(
            child: Text('Hi Everybody!'),
          ),
      backgroundColor: Colors.blueGrey[900],
    ),
    body: Center(
      child: Image(
        image: AssetImage(
          'images/diamond.png',
        ),
      ),
    ),
  ),
);
}
}

Upvotes: 2

Related Questions