0

I am making an app in which users can fill a form and can save their financial transaction details for every transaction they make, for this, I want to add a date as well and also I want to fetch data using the date as well. I am creating my database as follows:

await database.execute(
      "CREATE TABLE $TABLE_EXPENSES ("
      "$COLUMN_ID INTEGER PRIMARY KEY,"
      "$COLUMN_NAME TEXT,"
      "$COLUMN_AMOUNT INTEGER,"
      "$COLUMN_UNNECESSARYEXPENSES INTEGER,"
      "$COLUMN_CATEGORY TEXT,"
      "$COLUMN_DATETIME TEXT"
      ")",
    );

I am mapping as follows:

DateTime dateTime;
String pickedDate;

expense({this.id, this.name, this.amount, this.isUnnecessaryExpenses, this.category, this.dateTime, this.pickedDate}); 

Map toMap() {
var map = {
  DatabaseProvider.COLUMN_NAME: name,
  DatabaseProvider.COLUMN_AMOUNT: amount,
  DatabaseProvider.COLUMN_UNNECESSARYEXPENSES: isUnnecessaryExpenses ? 1 : 0,
  DatabaseProvider.COLUMN_CATEGORY: category,
  DatabaseProvider.COLUMN_DATETIME: pickedDate,
};

if (id != null) {
  map[DatabaseProvider.COLUMN_ID] = id;
}

return map;
}

expense.fromMap(Map map) {
  id = map[DatabaseProvider.COLUMN_ID];
  name = map[DatabaseProvider.COLUMN_NAME];
  amount = map[DatabaseProvider.COLUMN_AMOUNT];
  isUnnecessaryExpenses = map[DatabaseProvider.COLUMN_UNNECESSARYEXPENSES] == 1;
  category= map[DatabaseProvider.COLUMN_CATEGORY];
  pickedDate = map[DatabaseProvider.COLUMN_DATETIME];
}

I have also created the form in which user can save data and the date picker is as follows:

DateTime _dateTime;
String pickedDate;

Widget DatePicker() {

 showDatePicker(context: context, initialDate: DateTime.now(), firstDate: DateTime.parse("2020-01-01 00:00:01Z"), lastDate: DateTime.now()
).then((value){
  if (value == null){
    return;
  }
  _dateTime = value;
  pickedDate = DateFormat('yyyy-MM-dd').format(_dateTime);
  });
  }

Now I want to know if it's the right method for saving the date in the SQLite flutter database because I have been trying to get a query running and I am unable to do it. Please follow this link to my another question Fetching data from sql databse in flutter datewise?

Please help. Thanks for your replies.

Sanchay Kasturey
  • 161
  • 1
  • 17

1 Answers1

0

That is the correct way which I was using everything was correct, turns out the problem was with adding single quotes in dates that's why it was not working. Now everything is working.

Sanchay Kasturey
  • 161
  • 1
  • 17