问题
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.
回答1:
This is what you're looking for
In File -> Settings -> Keymap, you should assign some key shortcut for that to happen. Default it will be Ctrl+\
回答2:
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.
回答3:
in the terminal, run
flutter doctor -v
flutter upgrade
Also, try File > Invalidate Caches / Restart... from Android studio menu
回答4:
Try This
I found 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';
then goto Tool > Flutter > Flutter Clean
回答5:
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!!!")),
);
}
}
回答6:
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!!!"));
}
}
回答7:
I had the same problem. To solve it you need to update flutter sdk.
回答8:
This works if all else fails: 1. Close Android Studio and the Simulators
Delete Android Studio by opening the Finder, click Applications, right click on Android Studio and click Move to Trash
Remove Android files: $ cd ~/Library/Android $ rm -fr * $ cd $ rm -fr .android
Remove Java https://www.java.com/en/download/help/mac_uninstall_java.xml
Install Java
Install and Launch Android Studio and accept the license agreements https://developer.android.com/studio/install
回答9:
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.
回答10:
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'),
);
}
}
回答11:
- Goto android studio and click help and select check for updates
- Now click on update button.
- After restart goto plugins and reinstall the flutter plugin by uninstalling and reinstalling the pugins.
回答12:
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.
来源:https://stackoverflow.com/questions/57127496/flutter-hot-reloading-not-working-in-android-studiomac