[英]Using optional parameter in widget
I am getting a variable to a widget optionally and i want to use it.我可以选择获取一个小部件的变量,我想使用它。
class MainPage extends StatefulWidget {
final String? uyeId;
MainPage({
Key? key,
this.uyeId,
}) : super(key: key);
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int pageIndex = 0;
List<Widget> pageList = <Widget>[
Anasayfa(uyeId: widget.uyeId),
Mesajlar(),
Mesajlar(),
Bildirimler(
uyeId: "1999",
),
Hesabim()
];
@override
Widget build(BuildContext context) {
return MaterialApp(
Error is The instance member 'widget' can't be accessed in an initializer.错误是无法在初始化程序中访问实例成员“小部件”。 Try replacing the reference to the instance member with a different expression.
尝试用不同的表达式替换对实例成员的引用。 How i can use that parameter?
我如何使用该参数?
You can pass the value to the variable in initState like this:您可以像这样将值传递给 initState 中的变量:
late List<Widget> pageList;
initState:初始化状态:
@override
void initState() {
pageList = <Widget>[
Anasayfa(uyeId: widget.uyeId),
Mesajlar(),
Mesajlar(),
Bildirimler(
uyeId: "1999",
),
Hesabim()];
super.initState();
}
You can try this:你可以试试这个:
First create a variable of type list
:首先创建一个
list
类型的变量:
List<Widget> pageList = []
Second create method initState
:第二个创建方法
initState
:
void initState(){
super.initState();
}
Then you can create a function filling the list with data:然后您可以创建一个 function 用数据填充列表:
void fillingList(){
pageList = [
Anasayfa(uyeId: widget.uyeId),
Mesajlar(),
Mesajlar(),
Bildirimler(
uyeId: "1999",
),
Hesabim()
];
}
And finally you can call the function in the method initState:最后,您可以在方法 initState 中调用 function:
void initState(){
super.initState();
fillingList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.