pinchbv / floor

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

Unhandled Exception: type 'Null' is not a subtype of type 'double' in type cast #800

Closed ferut closed 8 months ago

ferut commented 8 months ago

Hi.

I have a Event Entity:

import 'package:floor/floor.dart';

@Entity(tableName: 'EventN') // Use @Entity instead of @entity
class EventN {
  @PrimaryKey(autoGenerate: true)
  final int? id;
  final DateTime date;
  final String description;
  final String category;
  final String subCategory;
  final String mdp;
  final double? amount;
  final String type;
  final String mdpType;
  final String? mdpName;
  EventN({this.id, required this.date, required this.description, required this.category, required this.amount, required this.type, required this.subCategory,required this.mdp, required this.mdpType, this.mdpName });

}

And a EventDao:

import 'package:floor/floor.dart';
import '../entity/event.dart';
@dao
abstract class EventDao {
  @Query('SELECT * FROM EventN')
  Stream<List<EventN>> findAllEvent();

  @Query('SELECT (SUM(CASE WHEN type = \'entrata\' THEN amount ELSE 0 END) - '
      'SUM(CASE WHEN type = \'uscita\' THEN amount ELSE 0 END)) AS net_result '
      'FROM EventN')
  Stream<double?> calculateNetResult();

  @insert
  Future<void> insertEventN(EventN event);
}

The problem is that when my table is empty, my query of calculate net_result gives error as Unhandled Exception: type 'Null' is not a subtype of type 'double' in type cast I shouldn't use databaseview in this case because it's a single result query right?

ferut commented 8 months ago

Solved It works. Query was wrong