简体   繁体   English

如何在 flutter 中从一个 dart 文件访问 static 列表到另一个文件?

[英]How to access the static list from one dart file to another in flutter?

Situation情况

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?这个怎么做?

And also并且

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM