我是Flutter的新手,我正在尝试为一种在线商店创建一个gridview。 JSON文件已上传到服务器,然后我通过其属性(如JSON Keys)创建了一个类。当我想实例化这些属性以使其喜欢image或text时,它显示此错误: 错误:无法使用静态访问权限访问实例成员'imageUrl'。 (位于[store] lib \ main.dart:82处的static_access_to_instance_member)
这是我的代码:
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;
}
答案 0 :(得分:1)
您需要创建一个类的实例,另外最好使用大写字母作为类名:
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;
}
然后做:
var poduct = Product.createProduct("name",1,"100","imageurl","off","desc");
print(poduct.imageUrl);