How to create a Column for double Values in flutter moor

Issue

I using flutter_moor for the SQLite Database. I have the issue that I need a column for a currency amount in double with two decimal places.

I find out that there is a RealColumn, but don’t find out how to implement it correct.

/// A column that stores floating point numeric values./// 

    abstract class RealColumn extends Column<double, RealType> {}

This is how the table looks like:

class MyClass extends Table {

    IntColumn get int => integer().autoIncrement()();

    TextColumn get title => text().withLength(min: 1, max: 35)();

    TextColumn get description => text().withLength(min: 0, max: 50)();

    TextColumn get categorie => text().withLength(min: 1, max: 35)();

    RealColumn get amount => double()();

    DateTimeColumn get date => dateTime()();
}

The double() at the RealColumn is not working. I thought it is the same like int or text.
I don’t find a solutions in the Documentation and the web.
My Question is, what is the right way to get the amount column working?

Thank you very much in advance for you help.

—-Edit:—-

Complete file:

import 'dart:core';

import 'package:moor/moor.dart';
import 'package:moor_flutter/moor_flutter.dart';

part 'moor_database.g.dart';

class Transactions extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text().withLength(min: 1, max: 35)();
  TextColumn get description => text().withLength(min: 0, max: 50)();
  TextColumn get categorie => text().withLength(min: 1, max: 35)();
  RealColumn get amount => double()();     //!!!<---This is line with the error///
  DateTimeColumn get date => dateTime()();
}

@UseMoor(tables: [Transactions])
class AppDatabase extends _$AppDatabase {
  AppDatabase()
      : super((FlutterQueryExecutor.inDatabaseFolder(
          path: 'db.sqlite',
          logStatements: true,
        )));

  @override
  int get schemaVersion => 1;

  Future<List<Transaction>> getAllTasks() => select(transactions).get();
  Stream<List<Transaction>> watchAllTasks() => select(transactions).watch();
  Future insertTask(Transaction transaction) => into(transactions).insert(transaction);
  Future updateTask(Transaction transaction) => update(transactions).replace(transaction);
  Future deleteTask(Transaction transaction) => delete(transactions).delete(transaction);
}

double()() is underlined

The Error Code is as followed (I’m using VSCode):

Abstract classes can’t be instantiated.
Try creating an instance of a subtype.

The expression doesn’t evaluate to a function, so it can’t be invoked.

‘$TransactionsTable.amount’ (‘GeneratedIntColumn Function()’) isn’t a valid override of ‘Transactions.amount’ (‘RealColumn Function()’).

For example the flutter_moor documentation for IntColumn is:

/// A column that stores int values.
abstract class IntColumn extends Column {}

This is working:

IntColumn get amount => integer()();

I don’t understand the error and why double()() is not working.

I hope this is helping 🙂

Solution

You should use real() instead of double() like this:

RealColumn get amount => real()(); 

Answered By – Samir N Ahmad

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.