[英]How to pass the value of an object within a Stateful widget?
如何将index
的值从Test
的构造函数传递到build
方法?
class Test extends StatefulWidget {
final int index; //How do I pass the value of this index....
const Test(this.index);
@override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
@override
Widget build(BuildContext context) {
getFollowers(index).then((response) { //..to here?
setState(() {
Iterable followerPlaceHolder = json.decode(response.body);
followerList =
followerPlaceHolder.map((model) => Data.fromJson(model)).toList();
});
});
return SafeArea(
child: Scaffold(
body: ExpansionTile(
title: const Text("Followers"),
children: [
ListView.builder(
itemCount: followerList.length,
scrollDirection: Axis.vertical,
itemBuilder: (context, value) {
return ListTile(
leading: CircleAvatar(
backgroundImage:
NetworkImage(followerList[value].avatarUrl),
),
);
},
),
],
),
),
);
}
}
您可以在构建方法中访问widget.index
。
只需使用widget.varName
(您的变量名)调用 statefulWidget class 中的任何值
只需使用 widget.index
class Test extends StatefulWidget {
final int index; //How do I pass the value of this index....
const Test(this.index);
@override
State<Test> createState() => _TestState();
}
class _TestState extends State<Test> {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Text(widget.index.toString()), // just use widget.index
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.