首页 > 解决方案 > 通过小部件构造函数传递数据

问题描述

我缺少关于构造函数的一小部分信息,我正在尝试通过小部件 PromotionCard( [ ... ] ) 传递一些数据 我在调试控制台中看不到任何错误 我尝试了 Cards 小部件和其他不同的东西来显示数据,但我无法找出下面这段代码有什么问题。

注意:当我打印 snapshot.data 时,我可以看到它完美返回

请提供任何帮助。

**promotions_page.dart**

class PromotionsPage extends StatefulWidget {
  @override
  _PromotionsPageState createState() => _PromotionsPageState();
}

class _PromotionsPageState extends State<PromotionsPage> {
  Future<Result> _promotionsResultsData;

  @override
  void initState() {
    _promotionsResultsData = PromotionApi().fetchPromotions();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
        child: ListView(
          physics: PageScrollPhysics(),
          children: [
            Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'Акции',
                      style: TextStyle(
                        fontSize: 18.0,
                        fontWeight: FontWeight.bold,
                        fontFamily: 'BuffetBold',
                      ),
                    ),
                    InkWell(
                      onTap: () => print('Archive promotion pressed!'),
                      child: Text(
                        'Архив Акции',
                        style: TextStyle(
                          fontSize: 16.0,
                          fontWeight: FontWeight.bold,
                          fontFamily: 'BuffetBold',
                          color: Colors.grey[400],
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 10.0,
                ),
                Container(
                  child: FutureBuilder<Result>(
                    future: _promotionsResultsData,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        return GridView.builder(
                          padding: EdgeInsets.zero,
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                            childAspectRatio: (45 / 35),
                            crossAxisCount: 1,
                          ),
                          shrinkWrap: true,
                          physics: ScrollPhysics(),
                          itemCount: snapshot.data.result.length,
                          itemBuilder: (BuildContext context, int index) {
                            //print(snapshot.data.result.length);
                            var promos = snapshot.data.result[index];
                            PromotionCard(
                                id: promos.id,
                                title: promos.title,
                                description: promos.description,
                                image: promos.image);
                          },
                        );
                      } else {}
                      return Center(
                        child: Text(
                          "Loading ...",
                          style: TextStyle(
                              fontWeight: FontWeight.w900, fontSize: 30.0),
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

我正在尝试将数据传递到此屏幕

**w_promotion_card.dart**
class PromotionCard extends StatelessWidget {
  final String id;
  final String title;
  final String description;
  final String image;

  PromotionCard({this.id, this.title, this.description, this.image});

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 200.0,
      margin: EdgeInsets.fromLTRB(0.0, 20.0, 0.0, 10.0),
      padding: EdgeInsets.zero,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: NetworkImage(image),
          alignment: Alignment.topCenter,
        ),
        borderRadius: BorderRadius.circular(10.0),
        border: Border.all(
          width: 1.5,
          color: Colors.grey[300],
        ),
      ),
      child: Container(
        width: MediaQuery.of(context).size.width,
        margin: EdgeInsets.zero,
        child: Padding(
          padding: EdgeInsets.fromLTRB(10, 170.0, 10.0, 10.0),
          child: Text(
            title,
            style: TextStyle(
              fontSize: 16.0,
              fontFamily: 'BuffetRegular',
            ),
          ),
        ),
      ),
    );
  }
}

标签: flutterdart

解决方案


你错过了退货声明

itemBuilder: (BuildContext context, int index) {
                            //print(snapshot.data.result.length);
                            var promos = snapshot.data.result[index];
                            // you have to return the widget
                            return PromotionCard(
                                id: promos.id,
                                title: promos.title,
                                description: promos.description,
                                image: promos.image);
                          },

推荐阅读