-1

我正在尝试 moor(flutter sqflite builder),我正在关注文档但不为我工作,如果有人实施,请提供帮助。pin 是表的名称。请帮助,thanx提前 错误:

{
    "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": []
}

例如这里的代码

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 文件

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

1 回答 1

1

您需要在定义中替换Pin为:PinsTable

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

这将创建一个名为Pin(不同于表类Pins)的数据类,您可以这样使用:

@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);
} 
于 2020-01-23T09:18:51.757 回答