flutter - 在初始化期间读取静态变量| flutter

标签 flutter dart flutter-layout

我一直在尝试将静态数据加载到ListViewBuilder中,我使用下面给出的相同代码成功实现了该功能,但现在无法正常工作。

LIST VIEW BUILDER

ListView.builder(
                scrollDirection: Axis.horizontal,
                shrinkWrap: true,
                itemCount: allProducts.length==null ? 0 : allProducts.length,
                itemBuilder: (BuildContext ctxt, int index) {
                  return new CustomCardProduct(
                    product:allProducts[index],
                    onPress: (){

                      );
                    },
                  );
                },
            ),

静态数据和存储在USER.DART文件中的类。
 List<Product> allProducts= [
    Product(title: 'Quran Classes',price: 5000,description: "Hello World",likes: 4,mainImage: 'assets/quranforkidssample.jpg',galleryImages: galleryImages,serviceType: 'Home Based',user: user1,category: categories[0]),
    Product(title: 'Quran Classes',price: 5000,description: "Hello World",likes: 4,mainImage: 'assets/quranforkidssample.jpg',galleryImages: galleryImages,serviceType: 'Home Based',user: user1,category: categories[0]),
    Product(title: 'Quran Classes',price: 5000,description: "Hello World",likes: 4,mainImage: 'assets/quranforkidssample.jpg',galleryImages: galleryImages,serviceType: 'Home Based',user: user1,category: categories[0]),
    Product(title: 'Quran Classes',price: 5000,description: "Hello World",likes: 4,mainImage: 'assets/quranforkidssample.jpg',galleryImages: galleryImages,serviceType: 'Home Based',user: user1,category: categories[0]),
    Product(title: 'Quran Classes',price: 5000,description: "Hello World",likes: 4,mainImage: 'assets/quranforkidssample.jpg',galleryImages: galleryImages,serviceType: 'Home Based',user: user1,category: categories[0]),

  ];
  User user1= User(
    id: 0,
    name: 'User Name',
    imageUrl: 'assets/momina.jpg',
    cnicPicture: '',
    contactNumber: '0335-2366331'  ,
    city: 'Karachi',
    followers: 100,
    following: 200,
    rating: 2.5,
    area: 'Defence',

  );
  List<Category> categories=[
    Category(name: 'Education',icon: FontAwesomeIcons.pen),
    Category(name: 'Education',icon: FontAwesomeIcons.hackerNews),
    Category(name: 'Education',icon: FontAwesomeIcons.pen),
    Category(name: 'Education',icon: FontAwesomeIcons.pen),
    Category(name: 'Education',icon: FontAwesomeIcons.pen),
    Category(name: 'Education',icon: FontAwesomeIcons.pen),
    Category(name: 'Education',icon: FontAwesomeIcons.pen),

  ];
  List<String> galleryImages = [
    'assets/quranforkidssample.jpg',
    'assets/quranforkidssample.jpg',
    'assets/quranforkidssample.jpg',
    'assets/quranforkidssample.jpg',
  ];







class User {
  final int id;
  final String name;
  final String imageUrl;
  final String cnicPicture;
  final String contactNumber;
  final String city;
  final String email;
  final int followers;
  final int following;
  final List<Product> productList;
  final double rating;
  final String area;

  User({this.area,this.id,this.name, this.imageUrl, this.cnicPicture, this.contactNumber, this.city, this.email, this.followers, this.following, this.productList, this.rating});

}

class Product{
  final String title;
  final int price;
  final String description;
  final Category category;
  final int likes;
  final User user;
  final String mainImage;
  final List<String> galleryImages;
  final serviceType;

  Product({this.serviceType,this.title, this.price, this.description, this.category, this.likes, this.user, this.mainImage, this.galleryImages});

}

class Category{
  final String name;

  final IconData icon;

  Category({this.name, this.icon});

}

加载应用程序后,在红色框中显示错误。

在初始化期间读取静态变量“所有产品”。
我不想在单独的文件夹中调用此数据文件,因为我将通过构造函数传递数据
我的allProduct对象列表为null!但为什么 ???

最佳答案

您所需要做的就是更改ListView.builder中的allProducts.data [index]而不是allproducts [index],以及allProducts.data.length而不是allProducts.length。

ListView.builder(
            scrollDirection: Axis.horizontal,
            shrinkWrap: true,
            itemCount: allProducts.data.length==null ? 0 : allProducts.data.length,
            itemBuilder: (BuildContext context, int index) {
              return new CustomCardProduct(
                product:allProducts.data[index],
                onPress: (){

                  );
                },
              );
            },
        ),

关于flutter - 在初始化期间读取静态变量| flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60270040/

相关文章:

dart - 打印变量超出范围不会产生错误

dart - 指定文件路径的语法(相对和绝对)

flutter - 流监听的竞争条件

flutter - Flutter 中的容器和列小部件有什么区别?

android - 为什么我的扩展磁贴扩展到整个屏幕

flutter 如何检测底部导航索引已更改

flutter - 如何显示 double 到 2 个小数点?

visual-studio-code - 如何将 Flutter 与 Genymotion 连接起来?

flutter - flutter 柱删除空间之间的子项

flutter - TextFormField横向 View 用 flutter 的键盘填充屏幕?