如何从一个飞镖文件到另一个飞镖文件中访问静态列表?

时间:2020-08-07 09:51:48

标签: list flutter

情况

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)
  ]; 

这是shoe.dart文件中的鞋子类

class Shoe {
  String brand;
  int quantity;
  String image;
  Shoe({String b, int q, String i}) {
    brand = b;
    quantity = q;
    image = i;
  }
}

现在,我想使用shoeBank文件中的secondScreen.dart列表。 该怎么做?

还有

我想在将参数传递给CheckOutItems中下面的组件secondScreen.dart的同时使用shoeBank的值

CheckoutItems(
addedToCartNumber:use of shoeBank Over here ,
checkOutScreenProductImage:use of shoeBank Over here,
shoesName:use of shoeBank Over here; 

如何执行此操作?

1 个答案:

答案 0 :(得分:1)

您可以使用static shoeBank来访问SecondScreen中的FirstScreen.shoeBank

我在下面添加了一个示例:

第二屏

 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,
     );
   }
 }

第一屏

 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(
       
     );
   }
 }