1

I want to get a stream of record which are in the same day of input (like all records with date of Feb 23, 2020).

Stream<List<BloodSugar>> watchBloodSugarsInDate(DateTime date) {

    return (select(bloodSugarsEntity)
          ..where((bp) => bp.date.equals(date)))
        .map((bloodSugarsEntity) => convertEntityToModel(bloodSugarsEntity))
        .watch();
  }

this is my code and it doesn't work because dateTime is combine of date and time. I tried using nested queries for separated comparison of year, month and day but I couldn't make it. Any help would be much appriciated.

7
  • take a look on this github.com/simolus3/moor/issues/304
    – John Joe
    Commented Feb 23, 2020 at 15:49
  • thanks. I update my method to this but it still doesn't work Stream<List<BloodSugar>> watchBloodSugarsInDate(DateTime dateTime) { return (select(bloodSugarsEntity) ..where((row) { final asDate = FunctionCallExpression('date', [row.date]); final year = FunctionCallExpression<String, StringType>( 'strftime', [const Constant<String, StringType>('%Y'), asDate]); final month = FunctionCallExpression<String, StringType>( 'strftime', [const Constant<String, StringType>('%m'), asDate]);
    – DNS
    Commented Feb 23, 2020 at 16:28
  • final day = FunctionCallExpression<String, StringType>( 'strftime', [const Constant<String, StringType>('%d'), asDate]); return year.equals(dateTime.year.toString()) & month.equals(dateTime.month.toString()) & day.equals(dateTime.day.toString()); })) .map((bloodSugarsEntity) => convertEntityToModel(bloodSugarsEntity)) .watch(); }
    – DNS
    Commented Feb 23, 2020 at 16:29
  • what is the value of date ?
    – John Joe
    Commented Feb 23, 2020 at 16:40
  • do you mean dateTime? there is no date in the function
    – DNS
    Commented Feb 23, 2020 at 16:50

2 Answers 2

2

You can get date from your row, and just compare with passed searchDate.

You have to compare year with year, month with month and day with day:

Stream<List<BloodSugar>> watchBloodSugarsInDate(DateTime searchDate) {
  return (select(bloodSugarsEntity)
        ..where(
          (row) {
            final date = row.date;

            return date.year.equals(searchDate.year) &
                date.month.equals(searchDate.month) &
                date.day.equals(searchDate.day);
          },
        ))
      .map((bloodSugarsEntity) => convertEntityToModel(bloodSugarsEntity))
      .watch();
}
1
  bool isSameDate(bp) {
    final value = bp.date;

    return value.year == date.year &&
        value.month == date.month &&
        value.day == date.day;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.