简体   繁体   English

如何在有状态小部件中传递 object 的值?

[英]How to pass the value of an object within a Stateful widget?

How do I pass the value of index from the constructor of Test to the build method?如何将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),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

You can access widget.index in the build method.您可以在构建方法中访问widget.index

just call any value in statefulWidget class with widget.varName (your variable name)只需使用widget.varName (您的变量名)调用 statefulWidget class 中的任何值

Just use widget.index只需使用 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.

相关问题 将有状态 class 的 state 的值传递给不同的小部件? - Pass value from state of stateful class to different widget? 如何将数据从有状态小部件传递到对话框? - How to pass data from stateful widget to Dialog Box? 如何访问从MaterialPageRoute传递的有状态小部件中的对象 - How-to access object in a stateful widget passed from MaterialPageRoute Flutter 将 json 数据传递给新的 Stateful Widget - Flutter pass json data to new Stateful Widget dart有状态小部件将数据传递给状态错误:小部件为null - dart stateful widget pass data to state error: widget is null 如何在 map 中存储有状态和无状态小部件列表以及如何访问特定的键值? - How can I store stateful and stateless widget list both in the map and how can access spesific key value? 如何在 Flutter 中从另一个有状态小部件更改一个有状态小部件的状态? - How to change state of one Stateful Widget from another Stateful Widget in Flutter? 如何将其他 dart 文件导入 flutter 中的有状态小部件? - How can I import other dart file to stateful widget in flutter? 如何更改 Stateful Widget 中 Switch 的 state,从 Provider 检索数据? - How to change the state of a Switch in a Stateful Widget, retrieving data from a Provider? 如何在 Flutter 中将事件从一个有状态小部件广播到另一个小部件 - How To Broadcast An Event From One Stateful Widget To Another In Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM