[英]How to access the static list from one dart file to another in flutter?
static list in firstScreen.dart
as below static list in
firstScreen.dart
如下
static List<Shoe> shoeBank = [
Shoe(b: "Red Shoe", i: "assets/nikeShoeProduct1.jpg", q: 0),
Shoe(b: "White Shoe", i: "assets/nikeShoeProduct2.jpg", q: 0)
];
this is the shoe class in shoe.dart
file这是鞋中的鞋
shoe.dart
文件
class Shoe {
String brand;
int quantity;
String image;
Shoe({String b, int q, String i}) {
brand = b;
quantity = q;
image = i;
}
}
Now I want to use the shoeBank
list in secondScreen.dart
file.现在我想使用
secondScreen.dart
文件中的shoeBank
列表。 How to do this?这个怎么做?
I want to use the values of shoeBank while passing a parameter to the component CheckOutItems
below in the secondScreen.dart
我想在将参数传递给 secondScreen.dart 中的组件
CheckOutItems
时使用secondScreen.dart
的值
CheckoutItems(
addedToCartNumber:use of shoeBank Over here ,
checkOutScreenProductImage:use of shoeBank Over here,
shoesName:use of shoeBank Over here;
How to do this?这个怎么做?
You can simply access the static shoeBank
in the SecondScreen
using FirstScreen.shoeBank
.您可以使用 FirstScreen.shoeBank 在
SecondScreen
中简单地访问static shoeBank
FirstScreen.shoeBank
。
I added an example below:我在下面添加了一个示例:
Second Screen第二屏
class SecondScreen extends StatelessWidget {
// access the static list using the class name
List<Shoe> shoeList = FirstScreen.shoeBank;
@override
Widget build(BuildContext context) {
// use the list here
return CheckoutItems(
// quantity
addedToCartNumber: shoeList[0].quantity,
// image
checkOutScreenProductImage: shoeList[0].image,
// shoe branch
shoesName: shoeList[0].brand,
);
}
}
First Screen第一个屏幕
class FirstScreen extends StatelessWidget {
// shoe bank static list here
static List<Shoe> shoeBank = [
Shoe(b: "Red Shoe", i: "assets/nikeShoeProduct1.jpg", q: 0),
Shoe(b: "White Shoe", i: "assets/nikeShoeProduct2.jpg", q: 0)
];
@override
Widget build(BuildContext context) {
return Container(
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.