大家好 我有一个模态类文件,名为:orderdetails.dart,这是代码:
class OrderDetails {
final shipmentNumber = "26";
}
我需要将ShipmentNumber数据作为文本传递给下面的小部件,
class MainPage extends StatefulWidget {
final OrderDetails orderDetails;
const MainPage({Key key, this.orderDetails}) : super(key: key);
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
OrderDetails orderDetails;
@override
void initState() {
super.initState();
// setState is not needed in initState
orderDetails = OrderDetails();
}
@override
Widget build(BuildContext context) {
Container(
height: 1000,
child: ListView.builder(
itemCount: int.parse(widget.orderDetails.shipmentNumber),
shrinkWrap: true,
itemBuilder: (ctx, int) {
return Card(
color: Color(0xFF1f2032),
elevation: 15,
child: Container(
width: 60,
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text(
widget.orderDetails.shipmentNumber,
style: TextStyle(color: Colors.lightGreenAccent),
),
Card(
color: Color(0xfffeaf0d),
child: Container(
height: 40,
width: 40,
child: Icon(
Icons.local_shipping,
color: Colors.white,
size: 25,
)),
),
,但它返回NotSuchMethodError,在Null上调用了吸气剂shipmentNumber 有没有办法从该文件中调用此方法?
答案 0 :(得分:0)
您使用的widget.orderDetails.shipmentNumber
为空,您正在使用orderDetails = OrderDetails();
设置状态字段
尝试this.orderDetails.shipmentNumber
或将此字段添加到构造函数中
答案 1 :(得分:0)
在调用 MainPage 类构造函数时,只需将"engines": {"yarn": "x.x"},
作为OrderDetails()
参数的参数,并从 _MainPageState 类中删除orderDetails
字段
这就是您应该调用 MainPage 类构造函数的方式。
orderDetails