Reading static variable during its initialization | Flutter

我是研究僧i 提交于 2021-02-11 02:00:19

问题


I have been trying to load static data into ListViewBuilder I successfully achieved the functionality using the same code given below but now it ain't working.

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: (){

                      );
                    },
                  );
                },
            ),

STATIC DATA AND CLASS STORED IN USER.DART FILE.

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

}

When the application has loaded, it gives an error in the Red box. saying Reading static variable "all products" during its initialization. I don't want to call this data file in the separate folders as I will be passing on data through constructors my allProduct Object List is null! but why ???


回答1:


All you need to do is change to allProducts.data[index] instead of allproducts[index], and allProducts.data.length instead of allProducts.length in your ListView.builder.

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: (){

                  );
                },
              );
            },
        ),


来源:https://stackoverflow.com/questions/60270040/reading-static-variable-during-its-initialization-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!