如何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),
),
);
},
),
],
),
),
);
}
}
uj5u.com熱心網友回復:
您可以widget.index在 build 方法中訪問。
uj5u.com熱心網友回復:
只需使用
widget.varName(您的變數名)呼叫 statefulWidget 類中的任何值
uj5u.com熱心網友回復:
只需使用 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
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/456619.html
