pinchbv / floor

The typesafe, reactive, and lightweight SQLite abstraction for your Flutter applications
https://pinchbv.github.io/floor/
Apache License 2.0
965 stars 191 forks source link

Error in code generation with @transaction method with return of nullable variable #783

Open DarkzyRB opened 1 year ago

DarkzyRB commented 1 year ago

I have this problem, which could be a bug (Or I'm doing something wrong):

I declare both methods in the Dao:

  @Query("SELECT * FROM tariff WHERE ((STRFTIME('%H', timeStart) <= STRFTIME('%H', (datetime('now','localtime'))) "
      "AND STRFTIME('%H', timeEnd) >= STRFTIME('%H', (datetime('now','localtime')))) "
      "OR (timeStart=:timeStart AND timeEnd=:timeEnd) ) LIMIT 1")
  Future<TariffModel?> getTariffWithTimeParameters(String timeStart, String timeEnd);

  @transaction
  Future<TariffModel?> getCurrentTariff() async {
    return await getTariffWithTimeParameters("00:00:00", "00:00:00");
  }

Then I generate the code with: flutter packages pub run build_runner build

But the generated code has an error in the second method:

  @override
  Future<TariffModel?> getCurrentTariff() async {
    if (database is sqflite.Transaction) {
      return super.getCurrentTariff();
    } else {
      return (database as sqflite.Database)
          .transaction<TariffModel>((transaction) async {  // ! Here the variable should be “TariffModel?”
        final transactionDatabase = _$AppDatabase(changeListener)
          ..database = transaction;
        return transactionDatabase.accountDao.getCurrentTariff(); // ! ERROR: The return type 'Future<TariffModel?>' isn't a 'Future<TariffModel>', as required by the closure's context.
      });
    }
  }

I'm using: Flutter 3.13.5 Dart 3.1.2

floor: ^1.4.2 build_runner: ^2.4.6 floor_generator: ^1.4.2

dkaera commented 1 year ago

Hi @DarkzyRB Thank you for reporting the issue. You're right, TariffModel should indeed be nullable in this case. As a temporary solution, I suggest using List<TariffModel> as the return type and creating a helper method that returns null if the list is empty.

mobile-pablo commented 1 month ago

SAME