将json repsonse转换为字符串并将其放入结构中

将json repsonse转换为字符串并将其放入结构中,json,api,mobile,flutter,dart,Json,Api,Mobile,Flutter,Dart,我正在制作一个购物手机应用程序 我想用json online中的数据替换静态数据,并将这些数据放在一个结构中,使用Gridview显示,然后在其他地方使用 这就是结构: class Product { String _urlToImage; String _about; String _title; double _price; double _weight; int _id; Product(this._urlToImage, this._title,

我正在制作一个购物手机应用程序

我想用json online中的数据替换静态数据,并将这些数据放在一个结构中,使用Gridview显示,然后在其他地方使用 这就是结构:

    class Product {

  String _urlToImage;
  String _about;
  String _title;
  double _price;
  double _weight;
  int _id;


  Product(this._urlToImage, this._title, this._price, this._weight, this._id){
    _about = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
  }

  double get weight => _weight;

  double get price => _price;

  String get title => _title;

  String get urlToImage => _urlToImage;

  int get id => _id;

  String get about => _about;


}
```
这是结构内部的本地数据

  class ProductsRepository{

List<Product>  fetchAllProducts() {

return [
  new Product("assets/images/spelt_noodles.png", "Biona Organic Spelt Noodles", 2.99, 250, 0),
  new Product("assets/images/spelt_italian.png", "Biona Organic Spelt Fusili Brown", 2.35, 500, 1),
  new Product("assets/images/spelt_spaghetti.png", "Biona Organic Whole Spelt Spaghetti", 2.35, 500, 2),
  new Product("assets/images/spelt_tagliatelle.png", "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 3),
  new Product("assets/images/spelt_penne.png", "Biona Organic Whole Spelt Penne", 2.35, 500, 4),
  new Product("assets/images/spelt_tagliatelle.png", "Biona Organic Spelt Spinach Artisan Tagliatelle", 1.99, 250, 5),
  new Product("assets/images/spelt_fusilli.png", "Biona Organic Spelt Fusilli Tricolore", 1.99, 250, 6),
];}
就像这个概念一样,但是json数据应该在Product()结构中


这是解析数据的方法

    List<Product> list = new List<Product>();
    var map= json.decode(response.body);

    for(var item in map){
      Product product = new Product.fromJson(item); //making an assumption how your json looks
      list.add(product); //user a Futurebuilder to create your GridView
    }
List List=新列表();
var map=json.decode(response.body);
for(映射中的变量项){
Product Product=new Product.fromJson(item);//假设json的外观
list.add(product);//使用Futurebuilder创建GridView
}
这是定义模型类的方法

    Product fromJson(Map<String, dynamic> json){
      Product product = new Product(
        _urlToImage = json['urlToImage'] as String;
        _about = json['about'] as String;
        _title = json['title'] as String;
        _price = json['price'] as double;
        _weight = json['weight'] as double;
        _id = json['id'] as int;
      );
      return product;
    }

    ...
Product fromJson(映射json){
产品=新产品(
_urlToImage=json['urlToImage']作为字符串;
_about=json['about']作为字符串;
_title=json['title']作为字符串;
_price=json['price']为双精度;
_权重=json['weight']为双精度;
_id=json['id']作为int;
);
退货产品;
}
...

Gridview示例

那么这里的问题是什么,您想知道如何实现Gridview或将json转换为产品对象吗?什么不起作用?错误是什么?正如我在回答中提到的,没有看到Json,我不得不猜测。你的问题和回答非常模糊。这都是关于让这些产品(…)数据来自我的json,而不是已经存在的数据,所以每次我从API添加一个新产品时,它都会显示在应用程序中。你可以在你的原始帖子中添加一个json片段,这样我们就可以看到它的结构。另外,如果你可以粘贴错误信息或使用imgur截图,也可以在回复中发布。我希望你现在能理解我,谢谢。测试代码时,您是否更改了my map代码以匹配json属性?因为它是区分大小写的。我最初是从caps开始的,但我现在编辑它以匹配您的json,所以您现在创建产品时不应该有问题。使用调试器断点查看其工作方式。
[
    {
        "urlToImage": "assets/images/spelt_noodles.png",
        "title": "Biona Organic Spelt Noodles",
        "price": 2.99,
        "weight": 250,
        "id": 1,
        "created_at": "2019-07-07 10:44:53",
        "updated_at": "2019-07-07 10:44:53"
    },
    {
        "urlToImage": "assets/images/spelt_noodles.png",
        "title": "Biona Organic Spelt Noodles",
        "price": 2.99,
        "weight": 250,
        "id": 2,
        "created_at": "2019-07-07 10:44:53",
        "updated_at": "2019-07-07 10:44:53"
    },
    {
        "urlToImage": "assets/images/spelt_noodles.png",
        "title": "Biona Organic Spelt Noodles",
        "price": 2.99,
        "weight": 250,
        "id": 3,
        "created_at": "2019-07-07 10:44:53",
        "updated_at": "2019-07-07 10:44:53"
    },


]
    List<Product> list = new List<Product>();
    var map= json.decode(response.body);

    for(var item in map){
      Product product = new Product.fromJson(item); //making an assumption how your json looks
      list.add(product); //user a Futurebuilder to create your GridView
    }
    Product fromJson(Map<String, dynamic> json){
      Product product = new Product(
        _urlToImage = json['urlToImage'] as String;
        _about = json['about'] as String;
        _title = json['title'] as String;
        _price = json['price'] as double;
        _weight = json['weight'] as double;
        _id = json['id'] as int;
      );
      return product;
    }

    ...