我有这样的模型:

模型

class ActivityModel {
  String idActivity;
  String titleActivity;
  String dateTimeActivity;
  int isDoneActivity;
  int codeIconActivity;
  String informationActivity;
  String createdDateActivity;

  ActivityModel({
    this.idActivity,
    this.titleActivity,
    this.dateTimeActivity,
    this.isDoneActivity,
    this.codeIconActivity,
    this.informationActivity,
    this.createdDateActivity,
  });

ActivityModel.fromSqflite(Map<String, dynamic> map)
      : idActivity = map['id_activity'],
        titleActivity = map['title_activity'],
        dateTimeActivity = map['datetime_activity'],
        isDoneActivity = map['is_done_activity'],
        codeIconActivity = map['code_icon_activity'],
        informationActivity = map['information_activity'],
        createdDateActivity = map['created_date'];

  Map<String, dynamic> toMapForSqflite() {
    return {
      'id_activity': this.idActivity,
      'title_activity': this.titleActivity,
      'datetime_activity': this.dateTimeActivity,
      'is_done_activity': this.isDoneActivity,
      'code_icon_activity': this.codeIconActivity,
      'information_activity': this.informationActivity,
      'created_date': this.createdDateActivity,
    };
  }


我想获取 dateTimeActivity 早于日期的数据,然后使用此代码更新 isDoneActivity = 1:

源代码
final passedDateItem = _selectedActivityItem.where((element) {
      DateTime convertStringToDateTime =
          DateTime.parse(element.dateTimeActivity);
      return convertStringToDateTime.isBefore(DateTime.now());
    });
  if (passedDateItem != null) {
      print('Not Null');
    } else {
      print('Nulledd');
      return null;
    }

问题是, passedDateItem 返回Iterable [ActivityModel],是否可以将其转换为 ActivityModel ?这样我就可以像这样轻松更新吗?
 if (passedDateItem != null) {
      passedDateItem.isDoneActivity = 1; <<<
      // return passedDateItem.map((e) => e.isDoneActivity = 1);
      //  final testtt= passedDateItem.
      print('Not Null');
    } else {
      print('Nulledd');
      return null;
    }

最佳答案

遍历passedDateItem

for (var activityModel in passedDateItem) {
  //..conditions
  activityModel.isDoneActivity = 1;
}

如果您只对passedDateItem的第一个/最后一个元素感兴趣
使用
passedDateItem.first.isDoneActivity == 1
要么
passedDateItem.last.isDoneActivity == 1
确保在这种情况下passedDateItem不为空。

09-27 00:48