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?
CodePudding user response:
You can pass the value to the variable in initState like this :
late List<Widget> pageList;
initState :
@override
void initState() {
pageList = <Widget>[
Anasayfa(uyeId: widget.uyeId),
Mesajlar(),
Mesajlar(),
Bildirimler(
uyeId: "1999",
),
Hesabim()];
super.initState();
}
CodePudding user response:
You can try this:
First create a variable of type list
:
List<Widget> pageList = []
Second create method initState
:
void initState(){
super.initState();
}
Then you can create a function filling the list with data:
void fillingList(){
pageList = [
Anasayfa(uyeId: widget.uyeId),
Mesajlar(),
Mesajlar(),
Bildirimler(
uyeId: "1999",
),
Hesabim()
];
}
And finally you can call the function in the method initState:
void initState(){
super.initState();
fillingList();
}