Click here to Skip to main content
15,867,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with an expansion tile in a flutter that shows user information in an irregular hierarchical order.

I am optimizing the code to increase the performance of the expansion tile widget. Unit-III hierarchy differs from the rest and I am having a difficult time creating data classes from it. Any reference to dealing with multidimensional arrays effectively will be very helpful.

What I have tried:

JSON:

<pre>
{
    "status": true,
    "message": "",
    "data": {
        "1. UNIT-I CALCULUS": {
            "1.1 Limit and Continuity": {
                "is_lesson": true,
                "lesson_detail": [
                    {
                        "title": "Eg 1",
                        "lesson_id": "34670",
                        "course_id": "126",
                        "section_id": "2145",
                        "duration": "00:01:08",
                        "is_lesson_free": "0",
                        "video_url": "https://lb.mero.school/api/v1/video/retrieve_path/7f1814c2-bc5a-48b7-9b5a-4e493f564a03",
                        "video_type": "bulk_videos",
                        "attachment_type": "url",
                        "attachment": null
                    }
                ],
                "2. UNIT-II ALGEBRA": {
                    "2.1 Complex Numbers": {
                        "is_lesson": true,
                        "lesson_detail": [
                            {
                                "title": "Eg 1",
                                "lesson_id": "37069",
                                "course_id": "126",
                                "section_id": "2145",
                                "duration": "00:02:32",
                                "is_lesson_free": "0",
                                "video_url": "https://lb.mero.school/api/v1/video/retrieve_path/d42d03a3-c857-4e99-8439-edbda003d296",
                                "video_type": "bulk_videos",
                                "attachment_type": "url",
                                "attachment": null
                            }
                        ]
                    }
                },
                "3. UNIT-III VECTOR ALGEBRA": {
                    "is_lesson": true,
                    "lesson_detail": [
                        {
                            "title": "Q. 1",
                            "lesson_id": "33898",
                            "course_id": "126",
                            "section_id": "2145",
                            "duration": "00:02:42",
                            "is_lesson_free": "0",
                            "video_url": "https://lb.mero.school/api/v1/video/retrieve_path/5c6487a2-a3df-4175-b20c-57134d2684dd",
                            "video_type": "bulk_videos",
                            "attachment_type": "url",
                            "attachment": null
                        }
                    ]
                }
            }
        }
    }
}


Data Class:

import 'dart:convert';

import 'package:flutter/foundation.dart';

SectionDemoData sectionDemoDataFromMap(String str) =>
    SectionDemoData.fromMap(json.decode(str));

String sectionDemoDataToMap(SectionDemoData data) => json.encode(data.toMap());

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

  final bool status;
  final String message;
  final SubSection data;

  factory SectionDemoData.fromMap(Map<String, dynamic> json) => SectionDemoData(
        status: json["status"],
        message: json["message"],
        data: SubSection.fromMap(json["data"]),
      );

  Map<String, dynamic> toMap() => {
        "status": status,
        "message": message,
        "data": data.toMap(),
      };
}

class SubSection {
  SubSection({
    required this.lessonList,
    required this.lessonName,
    isLessonItself = false,
  });

  final List<Lesson> lessonList;
  final List<String> lessonName;
  bool isLessonItself = false;

  Map<String, dynamic> toMap() {
    final result = <String, dynamic>{};

    result.addAll({'lessonList': lessonList.map((x) => x.toMap()).toList()});

    return result;
  }

  factory SubSection.fromMap(Map<String, dynamic> map) {
    map.forEach(
      (key, value) {
        if (isLessonItself) print("KEY HERR:" + value);
      },
    );
    return SubSection(
      lessonName: List<String>.from(map => map.Key),
      lessonList:
          List.generate(map.keys.length, (index) => Lesson.fromMap(map[index])),
    );
  }

  String toJson() => json.encode(toMap());

  factory SubSection.fromJson(String source) =>
      SubSection.fromMap(json.decode(source));

  @override
  String toString() => 'SubSection(lesson: ,lessonList: $lessonList)';
}

class Lesson {
  Lesson({
    required this.isLesson,
    required this.lessonDetailList,
  });

  final bool isLesson;
  final List<LessonDetail> lessonDetailList;

  factory Lesson.fromMap(Map<String, dynamic> json) => Lesson(
        isLesson: json["is_lesson"],
        lessonDetailList: List<LessonDetail>.from(
            json["lesson_detail"].map((x) => LessonDetail.fromMap(x))),
      );

  Map<String, dynamic> toMap() => {
        "is_lesson": isLesson,
        "lesson_detail":
            List<dynamic>.from(lessonDetailList.map((x) => x.toMap())),
      };
}

class LessonDetail {
  LessonDetail({
    required this.title,
    required this.lessonId,
    required this.courseId,
    required this.sectionId,
    required this.duration,
    required this.isLessonFree,
    required this.videoUrl,
    required this.videoType,
    required this.attachmentType,
    required this.attachment,
  });

  final String title;
  final String lessonId;
  final String courseId;
  final String sectionId;
  final String duration;
  final String isLessonFree;
  final String videoUrl;
  final String videoType;
  final String attachmentType;
  final dynamic attachment;

  factory LessonDetail.fromMap(Map<String, dynamic> json) => LessonDetail(
        title: json["title"],
        lessonId: json["lesson_id"],
        courseId: json["course_id"],
        sectionId: json["section_id"],
        duration: json["duration"],
        isLessonFree: json["is_lesson_free"],
        videoUrl: json["video_url"],
        videoType: json["video_type"],
        attachmentType: json["attachment_type"],
        attachment: json["attachment"],
      );

  Map<String, dynamic> toMap() => {
        "title": title,
        "lesson_id": lessonId,
        "course_id": courseId,
        "section_id": sectionId,
        "duration": duration,
        "is_lesson_free": isLessonFree,
        "video_url": videoUrl,
        "video_type": videoType,
        "attachment_type": attachmentType,
        "attachment": attachment,
      };
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900