在flutter中返回上一页



我有一个flutter应用程序,通过按配对设备列表中的设备名称与设备通过蓝牙连接。这是代码:

final BluetoothDevice server;

DataCollectionPage({required this.server});
...............................................................................
child: ListView(
children: devices
.map((_device)=> BluetoothDeviceListEntry(
device: _device,
enabled: true,
onTap: (){
if (kDebugMode) {
print("item");
}
_startDataCollection(context, _device);
},
................................................................................
void _startDataCollection(BuildContext context, BluetoothDevice server){
Navigator.of(context).push(MaterialPageRoute(builder: (context){
return DataCollectionPage(server: server);
}));
}

然后导航到">DataCollectionPage";页面,我执行一些操作和数据收集方法,最后我将在另一个名为&; datacollectiontimer &;的页面。在这个页面中,计时器将在屏幕上显示几秒钟,然后在这个计时器结束时,一个对话框将显示一些消息,然后最后,一旦我按下按钮关闭这个对话框,我想回到datacolltionpage。所以如果我尝试使用

MaterialPageRoute( builder: (context) => DataCollectionPage(),   ),

它会给出一个错误,因为参数'服务器'是必需的,我从配对设备的列表中获得的是在一个不同的类。是否有一种方法可以从当前返回到DataCollectionPage,而不需要返回到配对设备列表所在的页面?

提前谢谢你

您需要将server字段设为可选,然后使用popUntil

final BluetoothDevice? server;
DataCollectionPage({this.server});

最新更新