I m trying moor (sqflite builder), I m following documents but not working for me, pls help if anyone implemented

Issue

I m trying moor (flutter sqflite builder), I m following documents but not working for me, pls help if anyone implemented.
The pin is the name of the table. pls help, thanx in advance
Error:

{
    "resource": "/D:/my project/snapexpenses/lib/Database/moor_database.dart",
    "owner": "dart",
    "code": "undefined_identifier",
    "severity": 8,
    "message": "Undefined name 'pins'.\nTry correcting the name to one that is defined, or defining the name.",
    "source": "dart",
    "startLineNumber": 42,
    "startColumn": 55,
    "endLineNumber": 42,
    "endColumn": 59,
    "tags": []
}

eg code here

import 'dart:io';
import 'package:moor/moor.dart';
import 'package:moor_ffi/moor_ffi.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
part 'moor_database.g.dart';

class Expenses extends Table{
    IntColumn get id => integer().autoIncrement()();
    TextColumn get name => text().withLength(min: 1, max: 180)();
    DateTimeColumn get createdAt => dateTime().nullable()();
}
class Pin extends Table{
    IntColumn get id => integer().autoIncrement()();
    TextColumn get name => text().withLength(min: 4)();
    DateTimeColumn get createdAt => dateTime().nullable()();
}
LazyDatabase _openConnection() {
// the LazyDatabase util lets us find the right location for the file async.
    return LazyDatabase(() async {
    // put the database file, called db.sqlite here, into the documents folder
    // for your app.
        final dbFolder = await getApplicationDocumentsDirectory();
        final file = File(p.join(dbFolder.path, 'db.sqlite'));
        return VmDatabase(file,logStatements: true);
    });
}
// tables we just defined. We'll see how to use that database class in a moment.
@UseMoor(tables: [Pin])
class SnapexpensesDatabase extends _$SnapexpensesDatabase {
// we tell the database where to store the data with this constructor
    SnapexpensesDatabase() : super(_openConnection());
// you should bump this number whenever you change or add a table definition. Migrations
// are covered later in this readme.
    @override
    int get schemaVersion => 1;
    Future insertTask(Insertable<PinData> pin) => into(pins).insert(pin);
} 

pubspec.yml file

dependencies:
  flutter:
    sdk: flutter

  moor: 2.2.0 # use the latest version
  moor_ffi: 0.3.1 # use the latest version
  path_provider: 1.5.1
  path: 1.6.4
  flutter_slidable: ^0.5.3

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

dev_dependencies:
  moor_generator: 2.2.0 # use the latest version
  build_runner:

Solution

You need to replace Pin by Pins in the Table definition :

class Pins extends Table {
    IntColumn get id => integer().autoIncrement()();
    TextColumn get name => text().withLength(min: 4)();
    DateTimeColumn get createdAt => dateTime().nullable()();
}

That will create a data class called Pin (different from table class Pins), which you can use this way :

@UseMoor(tables: [Pins])    // <-- Use Pins class here
class SnapexpensesDatabase extends _$SnapexpensesDatabase {
    SnapexpensesDatabase() : super(_openConnection());

    @override
    int get schemaVersion => 1;

    Future<int> insertTask(Pin pin) => into(pins).insert(pin);
} 

Answered By – Augustin R

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *