【问题标题】:Parse data from a array inside array and create new list based on condition of map value in FLUTTER从数组内部的数组中解析数据并根据 FLUTTER 中映射值的条件创建新列表
【发布时间】:2020-12-31 08:07:24
【问题描述】:
[
    {
        "name": "School1",
        "image": "url1",
        "place": "place1",
        "class": [
            {
                "class": "first",
                "strength": "25",
                "students": [
                    {
                        "id": "student1",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 0
                    },
                    {
                        "id": "student2",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 1
                    }
                ]
            },
            {
                "class": "second",
                "strength": 30,
                "students": [
                    {
                        "id": "student1",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 0
                    },
                    {
                        "id": "student2",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 1
                    }
                ]
            }
        ]
    },
    {
        "name": "School2",
        "image": "url2",
        "place": "place2",
        "class": [
            {
                "class": "first",
                "strength": "25",
                "students": [
                    {
                        "id": "student1",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 0
                    },
                    {
                        "id": "student2",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 1
                    }
                ]
            },
            {
                "class": "second",
                "strength": 30,
                "students": [
                    {
                        "id": "student1",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 0
                    },
                    {
                        "id": "student2",
                        "dob": "mdd:mm:yyyy",
                        "age": 6,
                        "fees": 1
                    }
                ]
            }
        ]
    }
]

参考以上Json数据

FLUTTER/DART中满足特定条件后如何创建添加数据。在这里,我需要一些关于单个代码文件的帮助。请帮帮我。

  1. 将整个数据保存到一个列表中。
  2. 在列表视图中按 id 和 dob 和年龄显示学生。
  3. 点击学生位置后,在另一个屏幕上显示学校名称、班级、班级实力、年龄、出生日期等详细信息。
  4. 创建另外两个列表视图,根据已付费或未付费条件(数据中已付费=1,未付费=0)显示学生姓名和学校名称。

【问题讨论】:

    标签: arrays flutter listview dart maplist


    【解决方案1】:

    你的问题需要很长的答案......

    我会尽量给你一些提示。 首先,根据您的 json 数据创建相应的 dart 类。

    我建议你看看:https://app.quicktype.io/ 它是一个“即时解析任何语言的json”,你可以把你的json代码解析成dart。

    此后我从您的 json 示例中生成的内容:

       // To parse this JSON data, do
    //
    //     final school = schoolFromMap(jsonString);
    
    import 'dart:convert';
    
    List<School> schoolFromMap(String str) => List<School>.from(json.decode(str).map((x) => School.fromMap(x)));
    
    String schoolToMap(List<School> data) => json.encode(List<dynamic>.from(data.map((x) => x.toMap())));
    
    class School {
        School({
            this.name,
            this.image,
            this.place,
            this.schoolClass,
        });
    
        final String name;
        final String image;
        final String place;
        final List<Class> schoolClass;
    
        School copyWith({
            String name,
            String image,
            String place,
            List<Class> schoolClass,
        }) => 
            School(
                name: name ?? this.name,
                image: image ?? this.image,
                place: place ?? this.place,
                schoolClass: schoolClass ?? this.schoolClass,
            );
    
        factory School.fromMap(Map<String, dynamic> json) => School(
            name: json["name"] == null ? null : json["name"],
            image: json["image"] == null ? null : json["image"],
            place: json["place"] == null ? null : json["place"],
            schoolClass: json["class"] == null ? null : List<Class>.from(json["class"].map((x) => Class.fromMap(x))),
        );
    
        Map<String, dynamic> toMap() => {
            "name": name == null ? null : name,
            "image": image == null ? null : image,
            "place": place == null ? null : place,
            "class": schoolClass == null ? null : List<dynamic>.from(schoolClass.map((x) => x.toMap())),
        };
    }
    
    class Class {
        Class({
            this.classClass,
            this.strength,
            this.students,
        });
    
        final String classClass;
        final dynamic strength;
        final List<Student> students;
    
        Class copyWith({
            String classClass,
            dynamic strength,
            List<Student> students,
        }) => 
            Class(
                classClass: classClass ?? this.classClass,
                strength: strength ?? this.strength,
                students: students ?? this.students,
            );
    
        factory Class.fromMap(Map<String, dynamic> json) => Class(
            classClass: json["class"] == null ? null : json["class"],
            strength: json["strength"],
            students: json["students"] == null ? null : List<Student>.from(json["students"].map((x) => Student.fromMap(x))),
        );
    
        Map<String, dynamic> toMap() => {
            "class": classClass == null ? null : classClass,
            "strength": strength,
            "students": students == null ? null : List<dynamic>.from(students.map((x) => x.toMap())),
        };
    }
    
    class Student {
        Student({
            this.id,
            this.dob,
            this.age,
            this.fees,
        });
    
        final String id;
        final String dob;
        final int age;
        final int fees;
    
        Student copyWith({
            String id,
            String dob,
            int age,
            int fees,
        }) => 
            Student(
                id: id ?? this.id,
                dob: dob ?? this.dob,
                age: age ?? this.age,
                fees: fees ?? this.fees,
            );
    
        factory Student.fromMap(Map<String, dynamic> json) => Student(
            id: json["id"] == null ? null : json["id"],
            dob: json["dob"] == null ? null : json["dob"],
            age: json["age"] == null ? null : json["age"],
            fees: json["fees"] == null ? null : json["fees"],
        );
    
        Map<String, dynamic> toMap() => {
            "id": id == null ? null : id,
            "dob": dob == null ? null : dob,
            "age": age == null ? null : age,
            "fees": fees == null ? null : fees,
        };
    }
    

    好的,现在您有了模型类。 这应该回答您的第 1 点。您可以将欢迎列表保存为 json,反之亦然。

    对于第 2 点,您应该使用 ListView.build() ,例如使用学生列表( List items = .... ; ):

      @override
      Widget build(BuildContext context) {
        assert(items != null);
        accounts.sort((a, b) => a.id.compareTo(b.id));
        return items.isNotEmpty
            ? ListView.builder(
                padding: const EdgeInsets.all(8),
                itemCount: items.length,
                itemBuilder: (context, counter) => Card(
        child: ListTile(
          onTap: () { // Open a new page/popup to display the student widget....
          },
          title: Text(item.id ?? ''),
          subtitle: Text(item.dob ?? ''),
        ),
              )
            : Container(
                child: Text('No items'),
              );
      }
    }
    

    这是用于显示卡片列表的构建方法,显示学生对象的简单标题。

    使用 onTap() {...} 方法显示学生详细信息。

    【讨论】:

    • 我有了基本的想法,让我来实现。谢谢...
    猜你喜欢
    • 2011-04-06
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2019-01-10
    • 2017-01-17
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多