hardik584 / Flutter-lunchbox

It's all about small crucial stuff
http://www.flutterlunchbox.tech
4 stars 0 forks source link

Add multiple photos to sqlite. #2

Closed rbrinda029 closed 4 years ago

rbrinda029 commented 4 years ago

import 'dart:convert';

import 'dart:io';

class PhotosAnswersData { int photoId = 0; String queId;

List imgPath; String createdAuditId; String date; String time; String createdAt; String updatedAt;

PhotosAnswersData.withId({ this.photoId, this.queId, this.imgPath, this.createdAuditId, this.date, this.time, this.createdAt, this.updatedAt, });

PhotosAnswersData({ this.photoId, this.queId, this.imgPath, this.createdAuditId, this.date, this.time, this.createdAt, this.updatedAt, });

factory PhotosAnswersData.fromJson(String str) => PhotosAnswersData.fromMap(json.decode(str));

String toJson() => json.encode(toMap());

factory PhotosAnswersData.fromMap(Map<String, dynamic> json) => PhotosAnswersData.withId( photoId: json["photo_id"] == null ? null : json["photo_id"], queId: json["que_id"] == null ? null : json["que_id"], // imgPath: json["img_path"] == null ? null : json["img_path"],

    imgPath: json["img_path"] == null ? null : List<String>.from(json["img_path"].map((x) => x)),
    createdAuditId:
        json["created_audit_id"] == null ? null : json["created_audit_id"],
    updatedAt: json["updated_at"] == null ? null : json["updated_at"],
    createdAt: json["created_at"] == null ? null : json["created_at"],
    date: json["date"] == null ? null : json["date"],
    time: json["time"] == null ? null : json["time"],
  );

Map<String, dynamic> toMap() => { "photo_id": photoId == null ? null : photoId, "que_id": queId == null ? null : queId, // "img_path": imgPath == null ? null : imgPath,

    "img_path": imgPath == null ? null : List<dynamic>.from(imgPath.map((x) => x)),
    "created_audit_id": createdAuditId == null ? null : createdAuditId,
    "updated_at": updatedAt == null ? null : updatedAt,
    "created_at": createdAt == null ? null : createdAt,
    "date": date == null ? null : date,
    "time": time == null ? null : time,
  };

}

// Create table String photosAnsSql = "CREATE TABLE $tablePhotosAns($photosAns_photo_id INTEGER PRIMARY KEY, $photosAns_que_id TEXT, " + "$photosAns_created_audit_id TEXT, $photosAns_img_path TEXT, " + "$col_date TEXT, $col_time TEXT, $col_created_at TEXT, $col_updated_at TEXT)";

//Insert data Future insertPhotos(PhotosAnswersData photosAnswersData) async { print("que_id type " + photosAnswersData.queId.runtimeType.toString());

Database db = await this.db;
var result = db.insert(tablePhotosAns, photosAnswersData.toMap());
// var result = db.rawInsert(
//     "INSERT INTO $tablePhotosAns (que_id, img_path, created_audit_id, updated_at, created_at, date, time) VALUES (${photosAnswersData.queId}, 'img_path', ${photosAnswersData.createdAuditId}, 'updated', 'created', 'date', 'time')");

return result;

}

rbrinda029 commented 4 years ago

Exception has occurred. SqfliteDatabaseException (DatabaseException(java.lang.String cannot be cast to java.lang.Integer) sql 'INSERT INTO Photos_Answers (photo_id, que_id, img_path, created_audit_id, updated_at, created_at, date, time) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)' args [1, [/storage/emulated/0/Android/data/com.trevor.seears/files/Pictures/35bd4580-56c6-432f-a80d-de9ea527317f1747453282.jpg], 1, 31-01-2020 03:02:15, 31-01-2020 03:02:15, 31-01-2020, 03:02:15]})

hardik584 commented 4 years ago

You can just seperate those imagepath value with , and then store in sqlflite it's easiest solution.