如何获取Navigator堆栈中的路由数量

ali*_*tor 1 flutter

有没有办法知道当前页面是否是导航器堆栈中的最后一个页面,并且Navigator.pop()此时调用将关闭应用程序?

Ahm*_*ama 6

您可以使用此代码检查路线是否是第一条:

ModalRoute.of(context).isFirst
Run Code Online (Sandbox Code Playgroud)

所以完整的代码是

if(! ModalRoute.of(context).isFirst)
Navigator.pop();
Run Code Online (Sandbox Code Playgroud)