Flutter路由传参的两种形式
第一种:
Navigator.of(context).pushNamed('/demo/page1',arguments: "flutter");
在PageState的Build函数里接收:
@override Widget build(BuildContext context) { var arguments = ModalRoute.of(context)!.settings.arguments; print("路由传递过来的参数:" + arguments.toString()); if(arguments.toString() == "flutter"){ _isRouteFromFlutter = true; }
return ...
}
然而实际开发中遇到需要根据路由参数值,调用接口请求的情况,如果在build里获取已经晚了一步。所有第二种在构造方法里接收
第二种:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Page1(routeFromFlutter:true),
),
);
接收参数:
class Page1 extends StatefulWidget {
final bool routeFromFlutter;
Page1({Key? key,required this.routeFromFlutter}) : super(key: key);
@override
_Page1PageState createState() => _Page1Page1State(routeFromFlutter);
}
class _Page1PageState extends State
{ bool _isRouteFromFlutter = false; _Page1PageState(this._isRouteFromFlutter);
... }
本站内容来源于作者发布和网络转载,如有版权相关问题请及时与我们取得联系,我们将立即删除。