Home > Software engineering >  how to fetch orginal_url from this api for flutter and dart
how to fetch orginal_url from this api for flutter and dart

Time:10-06

how to convert this JSON to dart/flutter and call it "orginal_url"? any time i want to call it it say it's null.

    {
"orders": {
    "order_items":[
    {
        "product_min":{
                "colors":[
                     {
                    "media":[
                          {
                         "original_url": b 
 "https://royalpetiq.com/royalpet2/public//storage/10628/msg5212971698-890.jpg",

                          }
                   ]
                }
              ]
           }
         }
      ]
  }
}

CodePudding user response:

Could you be more specific? I assume you want to convert the json string into a dart object. For that create a model class for the json and the use darts json module to serialize/deserialize it

CodePudding user response:

Copy the response from the request and paste it into the link I specified. Then take the model created on this site and paste it into a file in flutter. https://javiercbk.github.io/json_to_dart/

Then convert the returned response to the model you created

sample

 if (response.statusCode == 200) {
        var responseJson = jsonDecode(response.body);
        return YourJsonModel.fromJson(responseJson);
      }

CodePudding user response:

To parse this JSON data, do

final originalUrl = originalUrlFromJson(jsonString); and your model is:

import 'dart:convert';

OriginalUrl originalUrlFromJson(String str) => OriginalUrl.fromJson(json.decode(str));

String originalUrlToJson(OriginalUrl data) => json.encode(data.toJson());

class OriginalUrl {
    OriginalUrl({
        this.orders,
    });

    Orders orders;

    factory OriginalUrl.fromJson(Map<String, dynamic> json) => OriginalUrl(
        orders: Orders.fromJson(json["orders"]),
    );

    Map<String, dynamic> toJson() => {
        "orders": orders.toJson(),
    };
}

class Orders {
    Orders({
        this.orderItems,
    });

    List<OrderItem> orderItems;

    factory Orders.fromJson(Map<String, dynamic> json) => Orders(
        orderItems: List<OrderItem>.from(json["order_items"].map((x) => OrderItem.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "order_items": List<dynamic>.from(orderItems.map((x) => x.toJson())),
    };
}

class OrderItem {
    OrderItem({
        this.productMin,
    });

    ProductMin productMin;

    factory OrderItem.fromJson(Map<String, dynamic> json) => OrderItem(
        productMin: ProductMin.fromJson(json["product_min"]),
    );

    Map<String, dynamic> toJson() => {
        "product_min": productMin.toJson(),
    };
}

class ProductMin {
    ProductMin({
        this.colors,
    });

    List<Color> colors;

    factory ProductMin.fromJson(Map<String, dynamic> json) => ProductMin(
        colors: List<Color>.from(json["colors"].map((x) => Color.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "colors": List<dynamic>.from(colors.map((x) => x.toJson())),
    };
}

class Color {
    Color({
        this.media,
    });

    List<Media> media;

    factory Color.fromJson(Map<String, dynamic> json) => Color(
        media: List<Media>.from(json["media"].map((x) => Media.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "media": List<dynamic>.from(media.map((x) => x.toJson())),
    };
}

class Media {
    Media({
        this.originalUrl,
    });

    String originalUrl;

    factory Media.fromJson(Map<String, dynamic> json) => Media(
        originalUrl: json["original_url"],
    );

    Map<String, dynamic> toJson() => {
        "original_url": originalUrl,
    };
}
  • Related