How to convert sqllite date time into localtime via moor select api

126 views Asked by At

I am building a mobile application using flutter and moor as database library. I have to fetch all transactions for a particular month. My query is like below.

Future<List<Transaction>> fetchTransactionsByCategory(
  String name, DateTime reportDate) {
  return (select(transactions)
      ..where((i) {
        final date = i.date;
        return date.year.equals(reportDate.year) &
            date.month.equals(reportDate.month) &
            i.subCategory.equals(name);
      }))
    .get();

}

The issue is if the transaction time is May 01, 2021 12:00:00 AM, it is not coming under May report, instead it is coming under April report. I am able to understand in the database it is getting persisted as 30 Apr 2021 16:00:00 PM (timezone +8 here).

How I have to write the query to fetch the transactions based on local time zone? Any suggestions?

1

There are 1 answers

1
Ahmet KAYGISIZ On

In your pubspec intl: ^0.16.1 and import

import 'package:intl/intl.dart';

in your highest state(First state).

and then you can use it like ;

@override
void initState() {
 super.initState();
 initializeDateFormatting();
}