data.dart 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import 'package:vitalapp/pages/check/models/form.dart';
  2. List<FormObject> getFormObjectData(List<Map<String, dynamic>> formData) {
  3. List<FormObject> formObjectData = [];
  4. for (var i in formData) {
  5. formObjectData.add(FormObject.fromJson(i));
  6. }
  7. return formObjectData;
  8. }
  9. List<FormObject> updateChildren(List<Map<String, dynamic>> formData) {
  10. List<FormObject> updatedList = [];
  11. List<FormObject> formObjectDataList = getFormObjectData(formData);
  12. for (var i = 0; i < formObjectDataList.length; i++) {
  13. var item = formObjectDataList[i];
  14. var parentKey = item.parentKey;
  15. if (parentKey != null) {
  16. var parentIndex = -1;
  17. // 查找父对象的索引
  18. for (var j = 0; j < updatedList.length; j++) {
  19. if (updatedList[j].key == parentKey) {
  20. parentIndex = j;
  21. break;
  22. }
  23. }
  24. // 如果找到了父对象,则将当前对象放在父对象的"children"下面
  25. if (parentIndex != -1) {
  26. if (updatedList[parentIndex].children?.isNotEmpty ?? false) {
  27. // 检查当前对象是否已经在父对象的"children"中
  28. var existingChildIndex = -1;
  29. var children = updatedList[parentIndex].children;
  30. for (var k = 0; k < children!.length; k++) {
  31. if (children[k].key == item.key) {
  32. existingChildIndex = k;
  33. break;
  34. }
  35. }
  36. // 如果当前对象不在父对象的"children"中,则将其添加进去
  37. if (existingChildIndex == -1) {
  38. updatedList[parentIndex].children!.add(item);
  39. }
  40. } else {
  41. updatedList[parentIndex].children = [item];
  42. }
  43. } else {
  44. updatedList.add(item);
  45. }
  46. } else {
  47. updatedList.add(item);
  48. }
  49. }
  50. return updatedList;
  51. }