flutter - 通过小部件构造函数传递数据
问题描述
我缺少关于构造函数的一小部分信息,我正在尝试通过小部件 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',
),
),
),
),
);
}
}
解决方案
你错过了退货声明
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);
},
推荐阅读
- java - 在线程之间安全地发送类似数据包的对象,无需内部同步
- c++ - std::__gcd 和 std::gcd 有什么区别?
- c# - 如何在 TableClient.QueryAsync() [Azure.Data.Tables] 中筛选分页查询结果
- c++ - 如何使模板重载复制构造函数?
- odoo - (Odoo)显示来自 url 的图像而不将其存储在数据库中
- angular - 使用 ngFor 和 Map 进行角度变化检测
- python - 启动器中的 Python 虚拟环境致命错误:无法使用
- java - intellij 插件:在模块上添加 Jar 库
- swift - 如何为pdf设置标题和作者(swift,UIKit)
- python - 通过对一列进行分组并与另一列进行增量添加来创建列