从flutter中的本地sqlite文件读取futurebuilder snapshot.data为null

时间:2019-12-03 13:49:13

标签: sqlite listview flutter dart

即使数据库中有数据,我也面临着snap.data为null的问题。

下面的

是我在加载屏幕时尝试调用的函数。以前我正在获取记录,但是在添加了一些额外的字段(如“重复”列)后,它停止工作。


Future<List<LocalNotificationData>> getAllScheduleNotification() async {
    final db = await database;
    var res = await db.rawQuery('SELECT * FROM scheduleNotifications');
    print("res : $res");
    List<LocalNotificationData> list =
        res.isNotEmpty ? res.map((c) => LocalNotificationData.fromMap(c)).toList() : [];
        print("result : ${res.map((c) => LocalNotificationData.fromMap(c)).toList()}");
    if(res != null){
      return list;
    }
  }

要检查它们是否是数据库中的数据,我有print("res : $res");进行测试,输出如下:

flutter:res:[
   {
      id:1,
      notificationId:1,
      title:Legs,
      description:<ul><li>Loosen your necktie or belt.</li><li>Remove high-heels.</li></ul>,
      days:'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
      hour:3,
      time:0,
      repeat:1
   },
   {
      id:2,
      notificationId:1,
      title:Legs,
      description:<ul><li>Loosen your necktie or belt.</li><li>Remove high-heels.</li></ul>,
      days:'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
      hour:3,
      time:0,
      repeat:1
   },
   {
      id:3,
      notificationId:1,
      title:Legs,
      description:<ul><li>Loosen your necktie or belt.</li><li>Remove high-heels.</li></ul>,
      days:'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
      hour:3,
      time:0,
      repeat:1
   },
   {
      id:4,
      notificationId:1,
      title:Legs,
      description:<ul><li>Loosen your necktie or belt.</li><li>Remove high-heels.</li></ul>,
      days:'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
      hour:3,
      time:0,
      repeat:1
   },
   {
      id:5,
      notificationId:1,
      title:Legs,
      description:<ul><li>Loosen your necktie or belt.</li><li<…>
flutter:snapshot.data:null

这里肯定是iussue,因为下面的行确实有打印结果,但是从不打印。

List<LocalNotificationData> list =
        res.isNotEmpty ? res.map((c) => LocalNotificationData.fromMap(c)).toList() : [];

LocalNotificationData的代码如下:


class LocalNotificationData {
  static const String idField = 'id';
  static const String notificationotificationIdField = 'notificationotificationId';
  static const String titleField = 'title';
  static const String descriptionField = 'description';
  static const String daysField = 'days';
  static const String hourField = 'hour';
  static const String minuteField = 'minute';
  static const String repeatField = 'repeat';

  String id;
  int notificationId;
  String title;
  String description;
  String days;
  int hour;
  int minute;
  bool repeat;

  LocalNotificationData({this.id, this.notificationId, this.title, this.description, this.days, this.hour, this.minute, this.repeat});

   factory LocalNotificationData.fromMap(Map<String, dynamic> json) => new LocalNotificationData(
      id: json["id"].toString(),
      notificationId: json["notificationId"],
      title: json["title"],
      description: json["description"],
      days: json["days"],
      hour: int.parse(json["hour"]),
      minute: json["minute"] ?? 0,
      repeat: json["repeat"] ?? 0,
    );

  Map<String, dynamic> toMap() => {
    "id": id,
    "notificationId": notificationId,
    "title": title,
    "description": description,
    "days": days,
    "hour": hour,
    "minute": minute,
    "repeat": repeat,
  };

  LocalNotificationData.fromDb(Map<String, dynamic> json, String id) {
    this.id = id;
    this.notificationId = json[notificationotificationIdField];
    this.title = json[titleField];
    this.description = json[descriptionField];
    this.days = json[daysField];
    this.hour = json[hourField];
    this.minute = json[minuteField];
    this.repeat = json[repeatField];
  }

  Map<String, dynamic> toJson() {
    return {
      idField: this.id,
      notificationotificationIdField: this.notificationId,
      titleField: this.title,
      descriptionField: this.description,
      daysField: this.days,
      hourField: this.hour,
      minuteField: this.minute,
      repeatField: this.repeat,
    };
  }

  @override
  String toString() {
    return 'id: $id, title: $title, notificationotificationId: $notificationId, days: $days, hour: $hour, minute: $minute, repeat: $repeat';
  }
}

1 个答案:

答案 0 :(得分:1)

@ princeoo7,我认为您的fromMaptoMap不正确。应该是

factory LocalNotificationData.fromMap(Map<String, dynamic> json) => new LocalNotificationData(
      id: json[idField],
      notificationId: json[notificationotificationIdField],
      .......
    );

  Map<String, dynamic> toMap() => {
    idField: id,
    notificationotificationIdField: notificationId,
    ..........
  };

看看是否有帮助。