如何对具有不同层次结构的多维数组的数据进行建模



我正在处理一个以不规则层次顺序显示用户信息的flutter中的扩展瓦片。

我正在优化代码以提高扩展瓦片小部件的性能。第三单元的层次结构与其他层次结构不同,我很难从中创建数据类。任何关于有效处理多维数组的参考都将非常有用。

JSON:

{
"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
}
]
}
}
}
}
}

数据类别:

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,
};
}

我想我找到了解决方法。使用给定Map对象的键和值就可以了。对此进行的任何优化都将不胜感激。

代码:

import 'dart:convert';
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,
});
final List<Lesson> lessonList;
final List<String> lessonName;
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) {
return SubSection(
lessonName: List.from(map["data"].keys), //Modified Code
lessonList:
List<Lesson>.from(map["data"].values.map((x) => Lesson.fromMap(x))), //Modified Code
);
}
String toJson() => json.encode(toMap());
factory SubSection.fromJson(String source) =>
SubSection.fromMap(json.decode(source));
@override
String toString() =>
'SubSection(lesson: $lessonName,lessonList: ${lessonList.toString()})';
}
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,
};
}

最新更新