问题
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