How to create a Column for double Values in flutter moor

2,326

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

RealColumn get amount => real()(); 
Share:
2,326
SpiriDs
Author by

SpiriDs

Updated on November 28, 2022

Comments

  • SpiriDs
    SpiriDs over 1 year

    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 :)

    • Samir N Ahmad
      Samir N Ahmad about 4 years
      how is not it working? You should add more informations like the thrown error by sqlite or at least descripe your problem in more details.
    • SpiriDs
      SpiriDs about 4 years
      Sorry I'm quite new :)
  • SpiriDs
    SpiriDs about 4 years
    Hi Samir sorry for the late response. Thank you very much for your help!!! It works fine. After I looked at your answer I see that it should be real :). IntColumn = int ...RealColumn = real.
  • Samir N Ahmad
    Samir N Ahmad about 4 years
    Good, If this answer solved your problem you should mark it as accepted so other people can see it.