如何通过默认导航在其他页面访问字符串、图片列表数据
默认导航跨页面访问列表数据实现方案
完全用系统自带的默认导航能力就能跨页面传值,不需要额外引入插件、全局状态管理,操作步骤如下:
- 首先整理好你要跨页面用的数据,包括已经写好的CEO名称列表,和你提到的图片列表,直接在发起跳转的页面定义即可:
List<String> ceoname = [ "Elon Musk", "Tim Cook", "Guenter Butschek", "Sundar pichai", "Thierry Delaporte", "Salil Parekh", "William Cho", "Satya Nadella", "Kenichiro Yoshida", "James Quincey" ]; // 替换成你自己的图片资源路径/网络地址列表 List ceoImageList = [ "对应Elon Musk的图片地址", "对应Tim Cook的图片地址", // 按顺序补全剩下的图片项即可 ];
- 给需要接收数据的目标页面加对应的接收属性,通过构造函数传参是默认导航最直接的传值方式:
class CeoListPage extends StatelessWidget { final List<String> nameList; final List imageList; const CeoListPage({ super.key, required this.nameList, required this.imageList, }); @override Widget build(BuildContext context) { // 页面内直接调用nameList、imageList就能拿到你传过来的所有数据 return Scaffold( appBar: AppBar(title: const Text("CEO信息")), // 举个使用例子,直接遍历两个列表渲染内容 body: ListView.builder( itemCount: nameList.length, itemBuilder: (context, index) { return ListTile( leading: Image.network(imageList[index]), // 如果是本地资源就用Image.asset title: Text(nameList[index]), ); }, ), ); } }
- 在原页面执行默认导航跳转逻辑时,直接把两个列表作为参数传给目标页面即可:
// 跳转逻辑绑定在按钮点击或者对应触发事件里 Navigator.push( context, MaterialPageRoute( builder: (context) => CeoListPage( nameList: ceoname, imageList: ceoImageList, ), ), );
如果你项目里用的是命名路由的默认导航方案,不需要改其他逻辑,只要在pushNamed时把两个列表放到arguments参数里,到目标页面通过
ModalRoute.of(context)?.settings.arguments解析拿到对应数据就行,同样是默认导航自带的能力,不需要额外依赖。
内容的提问来源于stack exchange,提问作者Akashptl
相关产品推荐
相关产品推荐

