0

I can successfully display PartnerName, PartnerAddress which are in PartnerData[]. Now inside PartnerData there is another ArrayList DayList[] how can I print those arrayList data DayName,TimeFrom etc etc??

{
    "Status": "1",
    "Message": "",
    "Data": {
        "EncDietId": "pEl2B9kuumKRxIxLJO76eQ==",
        "DietName": "dietcian2",
        "Email": null,
        "Phone": null,
        "AlternatePhone": null,
        "Image": "http://myapi/Doctor/65AUCUE8RTD2UKBRV.jpg",
        "Description": null,
        "Fee": null,
        "DiscountedFee": null,
        "BookingFee": null,
        "VisitDay": null,
        "TimeFrom": null,
        "TimeTo": null
    },
    "PartnerData": [
        {
            "PartnerId": "13",
            "EncPartnerId": "65gtodyhbtdInTsJWr1ZkA==",
            "PartnerName": "Rasomoy pvt. Hospital",
            "PartnerAddress": "Kol,Kol,Kol,Wb",
            "Fee": "1200",
            "DiscountedFee": "900",
            "BookingFee": "500",
            "DayList": [
                {
                    "DayName": "Wednesday",
                    "TimeFrom": "10:00",
                    "TimeTo": "16:00"
                },
                {
                    "DayName": "Friday",
                    "TimeFrom": "10:00",
                    "TimeTo": "16:00"
                },
                {
                    "DayName": "Saturday",
                    "TimeFrom": "10:00",
                    "TimeTo": "16:00"
                }
            ]
        }
     ]
    }

My Api

 Future<List<PartnerDatum>> dietcianDetailsApi() async {
var jsonResponse;

  var response = await http.post(
      Uri.parse("http://ggmyapin/api/api/Diet"),
      body: ({
        'EncId': encDietcianIdRef,
      }));
  if (response.statusCode == 200) {
    print("Correct");
    print(response.body);
    jsonResponse = json.decode(response.body.toString());
    print(jsonResponse);
      DietDetailsModel dataModel = dietDetailsModelFromJson(response.body);
    print(dataModel.partnerData.length);
    for (final item in dataModel.partnerData) 
    print(item.partnerName);

    List<PartnerDatum> arrData =dataModel.partnerData;
    return arrData;

  } else {
    print("Wrong Url");
    print(response.body);
    throw Exception("Faild to fetch");
  }

}

Displying in ListView. For now Successfully can display data PartnerName which are in PartnerData[] . I'm trying to display DayList[] data too. What will be the syntax for this?

  Container(
                  height: blockSizeVertical*30,//38
                  child: FutureBuilder(
                    future: dietcianDetailsApi(),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      // if (snapshot.connectionState !=ConnectionState.done) {
                      //   return CircularProgressIndicator();
                      // }
                      if (snapshot.hasError) {
                        return Text("Somthing went wrong");
                      }

                      if (snapshot.hasData) {
                        return ListView.builder(
                             scrollDirection: Axis.horizontal,
                              physics: BouncingScrollPhysics(),
                            shrinkWrap: true,
                            itemCount: snapshot.data.length,
                            itemBuilder: (BuildContext context, int index) =>
                              Container(
                                decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(10),
                              ),
                              width: blockSizeHorizontal*80,
                              margin: EdgeInsets.all(10),




                              child: Stack(children: [
                    Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [




                        SizedBox(height: blockSizeVertical*0.5),



                          Text(
                            'PartnerName :  ${snapshot.data[index].partnerName}',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: blockSizeHorizontal*3.5,
                              fontFamily: 'Poppins',
                              color: Theme.of(context).primaryColor,
                            ),
                            textAlign: TextAlign.center,
                          ),

Model Class

    // To parse this JSON data, do
//
//     final dietDetailsModel = dietDetailsModelFromJson(jsonString);

import 'package:meta/meta.dart';
import 'dart:convert';

DietDetailsModel dietDetailsModelFromJson(String str) => DietDetailsModel.fromJson(json.decode(str));

String dietDetailsModelToJson(DietDetailsModel data) => json.encode(data.toJson());

class DietDetailsModel {
    DietDetailsModel({
        required this.status,
        required this.message,
        required this.data,
        required this.partnerData,
    });

    String status;
    String message;
    Data data;
    List<PartnerDatum> partnerData;

    factory DietDetailsModel.fromJson(Map<String, dynamic> json) => DietDetailsModel(
        status: json["Status"],
        message: json["Message"],
        data: Data.fromJson(json["Data"]),
        partnerData: List<PartnerDatum>.from(json["PartnerData"].map((x) => PartnerDatum.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "Status": status,
        "Message": message,
        "Data": data.toJson(),
        "PartnerData": List<dynamic>.from(partnerData.map((x) => x.toJson())),
    };
}

class Data {
    Data({
        required this.encDietId,
        required this.dietName,
        required this.email,
        required this.phone,
        required this.alternatePhone,
        required this.image,
        required this.description,
        required this.fee,
        required this.discountedFee,
        required this.bookingFee,
        required this.visitDay,
        required this.timeFrom,
        required this.timeTo,
    });

    String encDietId;
    String dietName;
    dynamic email;
    dynamic phone;
    dynamic alternatePhone;
    String image;
    dynamic description;
    dynamic fee;
    dynamic discountedFee;
    dynamic bookingFee;
    dynamic visitDay;
    dynamic timeFrom;
    dynamic timeTo;

    factory Data.fromJson(Map<String, dynamic> json) => Data(
        encDietId: json["EncDietId"],
        dietName: json["DietName"],
        email: json["Email"],
        phone: json["Phone"],
        alternatePhone: json["AlternatePhone"],
        image: json["Image"],
        description: json["Description"],
        fee: json["Fee"],
        discountedFee: json["DiscountedFee"],
        bookingFee: json["BookingFee"],
        visitDay: json["VisitDay"],
        timeFrom: json["TimeFrom"],
        timeTo: json["TimeTo"],
    );

    Map<String, dynamic> toJson() => {
        "EncDietId": encDietId,
        "DietName": dietName,
        "Email": email,
        "Phone": phone,
        "AlternatePhone": alternatePhone,
        "Image": image,
        "Description": description,
        "Fee": fee,
        "DiscountedFee": discountedFee,
        "BookingFee": bookingFee,
        "VisitDay": visitDay,
        "TimeFrom": timeFrom,
        "TimeTo": timeTo,
    };
}

class PartnerDatum {
    PartnerDatum({
        required this.partnerId,
        required this.encPartnerId,
        required this.partnerName,
        required this.partnerAddress,
        required this.fee,
        required this.discountedFee,
        required this.bookingFee,
        required this.dayList,
    });

    String partnerId;
    String encPartnerId;
    String partnerName;
    String partnerAddress;
    String fee;
    String discountedFee;
    String bookingFee;
    List<DayList> dayList;

    factory PartnerDatum.fromJson(Map<String, dynamic> json) => PartnerDatum(
        partnerId: json["PartnerId"],
        encPartnerId: json["EncPartnerId"],
        partnerName: json["PartnerName"],
        partnerAddress: json["PartnerAddress"],
        fee: json["Fee"],
        discountedFee: json["DiscountedFee"],
        bookingFee: json["BookingFee"],
        dayList: List<DayList>.from(json["DayList"].map((x) => DayList.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "PartnerId": partnerId,
        "EncPartnerId": encPartnerId,
        "PartnerName": partnerName,
        "PartnerAddress": partnerAddress,
        "Fee": fee,
        "DiscountedFee": discountedFee,
        "BookingFee": bookingFee,
        "DayList": List<dynamic>.from(dayList.map((x) => x.toJson())),
    };
}

class DayList {
    DayList({
        required this.dayName,
        required this.timeFrom,
        required this.timeTo,
    });

    String dayName;
    String timeFrom;
    String timeTo;

    factory DayList.fromJson(Map<String, dynamic> json) => DayList(
        dayName: json["DayName"],
        timeFrom: json["TimeFrom"],
        timeTo: json["TimeTo"],
    );

    Map<String, dynamic> toJson() => {
        "DayName": dayName,
        "TimeFrom": timeFrom,
        "TimeTo": timeTo,

    };
    }

1 Answer 1

2

try below code hope its helpful to you

You can use string indexes to access these properties:

print(result['PartnerData'][0]['PartnerName']);        // Rasomoy pvt. Hospital
print(result['PartnerData'][0]['PartnerAddress']);   // Kol,Kol,Kol,Wb

Now go to Flutter documentation here:

The documentation also refers to the such model classes here :

Sign up to request clarification or add additional context in comments.

6 Comments

I can display PartnerName PartnerAddress, I want to display DayName which is in DayList under PartnerData
displaying PartnerName using 'PartnerName : ${snapshot.data[index].partnerName}',
${snapshot.data[index].dayList[index].dayName} seems to work
one more little help!! Can I get EncDietId too? As my Api returning Future<List<PartnerDatum>> But my EncDietId is not in PartnerDatum so how can I get those value?
you want EncDietId inside Data ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.