【问题标题】:ListView in flutter implement with json dataFlutter中的ListView用json数据实现
【发布时间】:2021-04-18 11:43:07
【问题描述】:

这是我的 json 字符串,我想知道如何使用它

{
    "status": 1,
    "message": "Data Received successful",
    "data": [
         {
            "billing_address": "BAGHPAT , BAGHPAT , UTTAR PRADESH - 250609",
            "delivery_address": "",
            "order_input": "0",
            "order_no": "Packaging BAWANA121020200001",
            "status": "Pending",
            "order_date": "12-10-2020",
            "order_time": "08:13:53",
            "total_quantity": 5,
            "gst": "09AUIOP",
            "shipp_address": null,
            "shipp_city": null,
            "shipp_state": null,
            "shipp_pincode": null,
            "shipp_mobile": null,
            "shipp_name": null,
            "bill_date": null,
            "challan": "",
            "bill": "",
            "expexted_deliverd_date": "19-10-2020"
        },

  ]
}
这是我的 json,我希望它在 Flutter 的 ListView 中调用

我在互联网上尝试了很多东西但是在这个颤动的图像中什么都不起作用,我想称之为 Json。请向我推荐与此 Json 相同的教程

【问题讨论】:

标签: flutter listview dart


【解决方案1】:

这是你要找的吗?

只需将您的回复粘贴到here,即可获取任何回复的模型


import 'dart:convert';

List<Model> modelFromJson(String str) => List<Model>.from(json.decode(str).map((x) => Model.fromJson(x)));

String modelToJson(List<Model> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Model {
    Model({
        this.billingAddress,
        this.deliveryAddress,
        this.orderInput,
        this.orderNo,
        this.status,
        this.orderDate,
        this.orderTime,
        this.totalQuantity,
        this.gst,
        this.shippAddress,
        this.shippCity,
        this.shippState,
        this.shippPincode,
        this.shippMobile,
        this.shippName,
        this.billDate,
        this.challan,
        this.bill,
        this.expextedDeliverdDate,
    });

    final String billingAddress;
    final String deliveryAddress;
    final String orderInput;
    final String orderNo;
    final String status;
    final String orderDate;
    final String orderTime;
    final int totalQuantity;
    final String gst;
    final dynamic shippAddress;
    final dynamic shippCity;
    final dynamic shippState;
    final dynamic shippPincode;
    final dynamic shippMobile;
    final dynamic shippName;
    final dynamic billDate;
    final String challan;
    final String bill;
    final String expextedDeliverdDate;

    factory Model.fromJson(Map<String, dynamic> json) => Model(
        billingAddress: json["billing_address"],
        deliveryAddress: json["delivery_address"],
        orderInput: json["order_input"],
        orderNo: json["order_no"],
        status: json["status"],
        orderDate: json["order_date"],
        orderTime: json["order_time"],
        totalQuantity: json["total_quantity"],
        gst: json["gst"],
        shippAddress: json["shipp_address"],
        shippCity: json["shipp_city"],
        shippState: json["shipp_state"],
        shippPincode: json["shipp_pincode"],
        shippMobile: json["shipp_mobile"],
        shippName: json["shipp_name"],
        billDate: json["bill_date"],
        challan: json["challan"],
        bill: json["bill"],
        expextedDeliverdDate: json["expexted_deliverd_date"],
    );

    Map<String, dynamic> toJson() => {
        "billing_address": billingAddress,
        "delivery_address": deliveryAddress,
        "order_input": orderInput,
        "order_no": orderNo,
        "status": status,
        "order_date": orderDate,
        "order_time": orderTime,
        "total_quantity": totalQuantity,
        "gst": gst,
        "shipp_address": shippAddress,
        "shipp_city": shippCity,
        "shipp_state": shippState,
        "shipp_pincode": shippPincode,
        "shipp_mobile": shippMobile,
        "shipp_name": shippName,
        "bill_date": billDate,
        "challan": challan,
        "bill": bill,
        "expexted_deliverd_date": expextedDeliverdDate,
    };
}

【讨论】:

    【解决方案2】:

    这只是解析您提供的 json 文件的基本示例。

    下面是你的 json 文件:

    {
        "status": 1,
        "message": "Data Received successful",
        "data": [
             {
                "billing_address": "BAGHPAT , BAGHPAT , UTTAR PRADESH - 250609",
                "delivery_address": "",
                "order_input": "0",
                "order_no": "Packaging BAWANA121020200001",
                "status": "Pending",
                "order_date": "12-10-2020",
                "order_time": "08:13:53",
                "total_quantity": 5,
                "gst": "09AUIOP",
                "shipp_address": null,
                "shipp_city": null,
                "shipp_state": null,
                "shipp_pincode": null,
                "shipp_mobile": null,
                "shipp_name": null,
                "bill_date": null,
                "challan": "",
                "bill": "",
                "expexted_deliverd_date": "19-10-2020"
            }
    
      ]
    }
    

    以下是我为您提供的 json 创建的模型。

    // To parse this JSON data, do
    //
    //     final model = modelFromJson(jsonString);
    
    import 'dart:convert';
    
    Model modelFromJson(String str) => Model.fromJson(json.decode(str));
    
    String modelToJson(Model data) => json.encode(data.toJson());
    
    class Model {
        Model({
            this.status,
            this.message,
            this.data,
        });
    
        int status;
        String message;
        List<Datum> data;
    
        factory Model.fromJson(Map<String, dynamic> json) => Model(
            status: json["status"],
            message: json["message"],
            data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
        );
    
        Map<String, dynamic> toJson() => {
            "status": status,
            "message": message,
            "data": List<dynamic>.from(data.map((x) => x.toJson())),
        };
    }
    
    class Datum {
        Datum({
            this.billingAddress,
            this.deliveryAddress,
            this.orderInput,
            this.orderNo,
            this.status,
            this.orderDate,
            this.orderTime,
            this.totalQuantity,
            this.gst,
            this.shippAddress,
            this.shippCity,
            this.shippState,
            this.shippPincode,
            this.shippMobile,
            this.shippName,
            this.billDate,
            this.challan,
            this.bill,
            this.expextedDeliverdDate,
        });
    
        String billingAddress;
        String deliveryAddress;
        String orderInput;
        String orderNo;
        String status;
        String orderDate;
        String orderTime;
        int totalQuantity;
        String gst;
        dynamic shippAddress;
        dynamic shippCity;
        dynamic shippState;
        dynamic shippPincode;
        dynamic shippMobile;
        dynamic shippName;
        dynamic billDate;
        String challan;
        String bill;
        String expextedDeliverdDate;
    
        factory Datum.fromJson(Map<String, dynamic> json) => Datum(
            billingAddress: json["billing_address"],
            deliveryAddress: json["delivery_address"],
            orderInput: json["order_input"],
            orderNo: json["order_no"],
            status: json["status"],
            orderDate: json["order_date"],
            orderTime: json["order_time"],
            totalQuantity: json["total_quantity"],
            gst: json["gst"],
            shippAddress: json["shipp_address"],
            shippCity: json["shipp_city"],
            shippState: json["shipp_state"],
            shippPincode: json["shipp_pincode"],
            shippMobile: json["shipp_mobile"],
            shippName: json["shipp_name"],
            billDate: json["bill_date"],
            challan: json["challan"],
            bill: json["bill"],
            expextedDeliverdDate: json["expexted_deliverd_date"],
        );
    
        Map<String, dynamic> toJson() => {
            "billing_address": billingAddress,
            "delivery_address": deliveryAddress,
            "order_input": orderInput,
            "order_no": orderNo,
            "status": status,
            "order_date": orderDate,
            "order_time": orderTime,
            "total_quantity": totalQuantity,
            "gst": gst,
            "shipp_address": shippAddress,
            "shipp_city": shippCity,
            "shipp_state": shippState,
            "shipp_pincode": shippPincode,
            "shipp_mobile": shippMobile,
            "shipp_name": shippName,
            "bill_date": billDate,
            "challan": challan,
            "bill": bill,
            "expexted_deliverd_date": expextedDeliverdDate,
        };
    }
    
    

    根据生成的模型,我已将 json 数据集成到 ui 中:

    import 'package:flutter/material.dart';
    
    
    import 'package:json_parsing_example/model2.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      var _isLoading = false;
    
      List<Datum> usersList = [];
    
      @override
      void initState() {
        super.initState();
    
        getData();
      }
    
      getData() async {
        setState(() {
          _isLoading = true;
        });
    
        String data =
            await DefaultAssetBundle.of(context).loadString("json/parse.json");
    
        final model = modelFromJson(data);
    
        usersList = model.data;
    
        setState(() {
          _isLoading = false;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            home: Scaffold(
          appBar: AppBar(
            title: Text("Users List "),
          ),
          body: _isLoading
              ? Center(
                  child: CircularProgressIndicator(),
                )
              : ListView.builder(
                  itemCount: usersList.length,
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int index) {
                    var item = usersList[index];
                    return Card(
                      child: Padding(
                        padding: EdgeInsets.all(5),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(item.billingAddress),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(item.orderNo),
                            ),
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(item.expextedDeliverdDate),
                            ),
                          ],
                        ),
                      ),
                    );
                  }),
        ));
      }
    }
    
    
    

    这是我提供的示例示例,您可以使用带有提供程序的 MVVM 或使用 bloc 的复杂方式,它在您身上。 我已经提到了一些文章,然后检查一下。 https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51 https://www.youtube.com/watch?v=wc8ecFSu5N8

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多