Reputation: 21
I'm new at Flutter and I'm trying to create a gridview for a kind of online store. A JSON file has been uploaded on server and then I've created a class by its properties like json Keys. When I want to Instance those properties to like image or text , it shows this error: error: Instance member 'imageUrl' can't be accessed using static access. (static_access_to_instance_member at [store] lib\main.dart:82)
this is my code:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'BottomNVG.dart';
import 'package:http/http.dart';
import 'product.dart';
void main() => runApp(MaterialApp(
debugShowCheckedModeBanner: false,
home:store()
));
class store extends StatefulWidget {
@override
_storeState createState() => _storeState();
}
class _storeState extends State<store> {
List<product> _items = [];
@override
void initState() {
// TODO: implement initState
super.initState();
fetchItems();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
title:Center(child: Text('Store')),
backgroundColor: Colors.red,
leading: Icon(Icons.arrow_back),
actions: <Widget>[
Icon(Icons.search),
],
),
body: Padding(
padding: EdgeInsets.all(8.0),
child: GridView.count(
crossAxisCount: 2,
crossAxisSpacing: 3,
mainAxisSpacing: 3,
children:
List.generate(_items.length,(int position){
return genearateItem();
}),
),
),
bottomNavigationBar:BottomNVG(),
floatingActionButton: FloatingActionButton(
child:Icon(Icons.add),
backgroundColor: Colors.red[900],
onPressed: (){},
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
void fetchItems() async{
var url = 'http://artamweb.ir/test/test.json';
Response response = await get(url);
setState(() {
var productJson = json.decode(utf8.decode(response.bodyBytes));
for (var i in productJson){
var productItem = product(i['prodct_name'],i['id'],i['price'],i['image_url'],i['off'],i['description']);
_items.add(productItem);
}
});
}
Card genearateItem(){
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
),
elevation: 3,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width:90,
height:90,
child:Image.network(product.imageUrl),
),
Text(product.productName,
style: TextStyle(
fontFamily:'Vazir',
color:Colors.red[900],
fontSize:14.0,
),
),
Text(product.price,
style: TextStyle(
fontFamily:'Vazir',
color:Color(0xFF575E67),
fontSize:14.0,
),
),
],
),
),
);
}
}
]2
product.dart
class product{
String _productName;
int _id;
String _price;
String _imageUrl;
String _off;
String _description;
product(this._productName, this._id, this._price, this._imageUrl, this._off,
this._description);
String get description => _description;
String get off => _off;
String get imageUrl => _imageUrl;
String get price => _price;
int get id => _id;
String get productName => _productName;
}
Upvotes: 1
Views: 1158
Reputation: 80934
You need to create an instance of the class, also it is better to use capital letter for the class name:
class Product{
String _productName;
int _id;
String _price;
String _imageUrl;
String _off;
String _description;
Product();
Product.createProduct(this._productName, this._id, this._price, this._imageUrl, this._off,
this._description);
String get description => _description;
String get off => _off;
String get imageUrl => _imageUrl;
String get price => _price;
int get id => _id;
String get productName => _productName;
}
Then do:
var poduct = Product.createProduct("name",1,"100","imageurl","off","desc");
print(poduct.imageUrl);
Upvotes: 1