【发布时间】:2019-06-07 23:46:47
【问题描述】:
将变量 (photoUrl) 传递给 Widget 的最佳方式是什么? 在状态类中添加小部件它说:在初始化程序中只能访问静态成员。
将方法更改为静态也不能解决我的问题。
class _ABState extends State<AB> {
int _selectedPage = 0;
String photoUrl; /* Value set in initState()*/
/* To switch between pages */
final _pageOptions = [
Text('Page 1'),
accountPage(), /* Page 2 */
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: _pageOptions[_selectedPage],
);
}
}
/* Widget outside class requires photoUrl */
Widget accountPage() {
return Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(photoUrl),
),
),
);
}
【问题讨论】: