0

I'm pulling my data from MongoDB with a Future. However, I want to pull only some of the data, not the whole data. How can I filter this?

I will put the data I filtered into the list. I added the readMongo and lessonModel codes.

In addition, although my codes work very well, I think that I did not do something according to the rules, if I am missing, I would be very happy if you could point out.

  Future<List<Map<String, dynamic>>> _getData() async {
    values = MongoDatabase.readMongo(MongoDatabase.lessonCollection);
    return values!;
  }

readMongo

static Future<List<Map<String, dynamic>>> readMongo(collectionName) async {
    final data = await collectionName.find().toList();
    return data;
  }

LessonModel

import 'dart:convert';

LessonModel lessonModelFromMap(String str) => LessonModel.fromMap(json.decode(str));

String lessonModelToMap(LessonModel data) => json.encode(data.toMap());

class LessonModel {
  LessonModel({this.id, this.info, this.subject});

  final Id? id;
  final Info? info;
  final List<Subject>? subject;

  factory LessonModel.fromMap(Map<String, dynamic> json) => LessonModel(
        info: json["info"] == null ? null : Info.fromMap(json["info"]),
        subject: json["subject"] == null ? null : List<Subject>.from(json["subject"].map((x) => Subject.fromMap(x))),
      );

  Map<String, dynamic> toMap() => {
        "_id": id == null ? null : id!.toMap(),
        "info": info == null ? null : info!.toMap(),
        "subject": subject == null ? null : List<dynamic>.from(subject!.map((x) => x.toMap())),
      };
}

class Id {
  Id({this.oid});

  final String? oid;

  factory Id.fromMap(Map<String, dynamic> json) => Id(oid: json["\u0024oid"]);

  Map<String, dynamic> toMap() => {"\u0024oid": oid};
}

class Info {
  Info({this.name, this.infoClass, this.semester, this.credit, this.icon});

  final String? name;
  final String? infoClass;
  final String? semester;
  final int? credit;
  final String? icon;

  factory Info.fromMap(Map<String, dynamic> json) => Info(
      name: json["name"],
      infoClass: json["class"],
      semester: json["semester"],
      credit: json["credit"],
      icon: json["icon"]);

  Map<String, dynamic> toMap() =>
      {"name": name, "class": infoClass, "semester": semester, "credit": credit, "icon": icon};
}

class Subject {
  Subject({this.subjectId, this.name, this.text, this.video, this.trainer});

  final int? subjectId;
  final String? name;
  final String? text;
  final String? video;
  final String? trainer;

  factory Subject.fromMap(Map<String, dynamic> json) => Subject(
      subjectId: json["subjectId"],
      name: json["name"],
      text: json["text"],
      video: json["video"],
      trainer: json["trainer"]);

  Map<String, dynamic> toMap() =>
      {"subjectId": subjectId, "name": name, "text": text, "video": video, "trainer": trainer};
}
  • 1
    share the contents of `readMongo` and `lessonCollection`. This is more of applying mongoDB logic than Flutter itself. Also, how are you accessing MongoDB inside Flutter. – Obum Dec 29 '22 at 09:28
  • 1
    First of all, you have an `async` function, and I don't see that you're doing `await` anywhere. Also, you should do an aggregation with `AggregationPipelineBuilder` and then pass the criteria that you want to do a filter upon. Take an example here -> https://stackoverflow.com/a/59486728/1737811 – mutantkeyboard Dec 29 '22 at 09:30
  • I share readMongo and lessonCollection –  Dec 29 '22 at 09:36

0 Answers0