颤振热重载其他页面

flutter hot reload other pages

我是flutter初学者
文本(printA())

PrintA(){print("A");返回 "A";}
在Flutter的A页面执行,热重载后控制台显示“A”。然后导航到页面 B 并将“B”也打印到控制台。这时候如果我hot reload的话,控制台上不仅会显示“B”,还会显示“A”。如果我重复屏幕导航,控制台上显示的 A 和 B 的数量将随着一次热重载而不断增加。这是什么原因?

class PageA extends StatelessWidget {
  const WordPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: MyDrawer(),
      appBar: AppBar(
        title: Text("PageA"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(printA()),
          ],
        ),
      ),
    );
  }
}

printA() {
  print("A");
  return "A";
}


此外,如果我只是在模拟器中进行屏幕导航而不进行热重载,则控制台上将像往常一样仅显示页面 A 上的“A”和页面 B 上的“B”。我使用 m1 mac.

A widget that manages a set of child widgets with a stack discipline.

Many apps have a navigator near the top of their widget hierarchy in order to display their logical history using an Overlay with the most recently visited pages visually on top of the older pages. Using this pattern lets the navigator visually transition from one page to another by moving the widgets around in the overlay. Similarly, the navigator can be used to show a dialog by positioning the dialog widget above the current page.

这是来自文档 https://api.flutter.dev/flutter/widgets/Navigator-class.html 您的新页面 'B' 就在上一页 'A' 之上,它从未被破坏过。热重载导致页面 'A' 运行 的构建方法再次出现,你得到 'A' 打印出来。