颤振)应用程序在热重载前不会处理



我现在研究颤振。但是我遇到了一个关于处理的问题。

首先我看到你我的代码。这段代码看起来很长,但并不难。

main.dart

import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FirstScreen(),
);
}
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// After hot reload, title will be changed to 'First Screen (After Hot Reload)'
title: Text('First Screen (Before Hot Reload)', style: TextStyle(fontWeight: FontWeight.bold)),
),
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) => SecondScreen()));
},
child: Text('Go To Second Screen', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
),
),
),
);
}
}

class SecondScreen extends StatefulWidget {
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> with TickerProviderStateMixin {
List<ScrollController> scrollControllers;
List<AnimationController> animationControllers;
List<FocusNode> focusNodes;
@override
void initState() {
super.initState();
scrollControllers = List.generate(10000, (_) => ScrollController());
animationControllers = List.generate(10000, (_) => AnimationController(vsync: this, duration: Duration(microseconds: 300)));
focusNodes = List.generate(10000, (_) => FocusNode());
}
@override
void dispose() {
scrollControllers.forEach((element) { element.dispose();});
animationControllers.forEach((element) { element.dispose();});
focusNodes.forEach((element) { element.dispose();});
print('No Get with dispose');
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// After hot reload, title will be changed to 'Second Screen (After Hot Reload)'
title: Text('Second Screen (Before Hot Reload)', style: TextStyle(fontWeight: FontWeight.bold)),
),
body: SafeArea(
child: Center(child: Text('Second Screen')),
),
);
}
}

我构建了这段代码并观察了这个简单应用程序的内存使用情况。

我的应用程序内存使用情况

我用这个步骤运行这个应用程序。

  1. 我以调试模式运行应用程序。
  2. 我转到第二个屏幕。(然后,该应用程序将在initState()方法中创建10,000个ScrollController,AnimationControllerFocusNode)
  3. 我回到第一个屏幕。(我认为这个应用程序应该在dispose()方法中处置ScrollController,AnimationControllerFocusNode。但它没有。)
  4. 我更改代码很少。就是AppBar的标题文本。并重新加载。然后,类似于应用程序的开始,内存使用已经改变。
  5. 我转到第二个屏幕。它创建了类似步骤2的东西。
  6. 我回到第一个屏幕。此时,它的处置是正确的。

问题这是我的问题。为什么这个应用程序不处理scrollControllers,animationConrollers,focusNodes在步骤3?然而,在热加载后,当它返回到第一个屏幕(步骤6)时,这个应用程序处理正确。这对我来说很奇怪。为什么在第一次构建应用程序不处置,但热加载后应用程序处置正确?


注)。我用IntelliJ.

'flutter——version'结果

Flutter 1.22.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 7891006299 (7 weeks ago) • 2020-12-10 11:54:40 -0800
Engine • revision ae90085a84
Tools • Dart 2.10.4

'flutter doctor'结果

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Version 10.0.19042.746], locale ko-KR)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[!] Android Studio (not installed)
[!] VS Code (version 1.52.1)
✗ Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)

如果您有需要对热加载进行特殊处理的任务,您可以提供一个reassemble钩子(类似于dispose)来运行您需要的代码:

// in your State widget:
@override
void reassemble {
debugPrint('Captain, we are going down!!!');
super.reassemble(); // must call
}

参见https://api.flutter.dev/flutter/widgets/State/reassemble.html。文档说这只适用于调试版本,因为当然,您不会在生产版本上热重新加载。:)

相关内容

  • 没有找到相关文章

最新更新