flutter:无法创建 moor_database.g.dart 文件

标签 flutter flutter-moor

我正在努力学习moor_flutter所以我在 pupspec.yaml 中添加了一些依赖项:

dependencies:
  flutter:
    sdk: flutter
  moor_flutter: ^2.1.1
  provider: ^4.0.4
  flutter_slidable: ^0.5.4
  path_provider: ^1.6.5
  path: ^1.6.4

  # 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.4.0
  build_runner: ^1.8.1
  flutter_test:
    sdk: flutter

安装这些依赖项后,我在 lib -> data -> moor_database.dart 文件中创建一个表类:

import 'package:moor_flutter/moor_flutter.dart';
part 'moor_database.g.dart';

class Tasks extends Table {
  IntColumn get id =>
      integer().autoIncrement().call();
  TextColumn get name => text().withLength(min: 1, max: 50)();
  DateTimeColumn get dueDate => dateTime().nullable()();
  BoolColumn get completed => boolean().withDefault(Constant(false))();
}

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

  @override
  int get schemaVersion => 1;
}

我想通过以下方式生成 dart 代码:

flutter packages pub run build_runner watch

但是我收到了这个错误:

$ flutter packages pub run build_runner watch
[INFO] Generating build script...
[INFO] Generating build script completed, took 349ms

[INFO] Setting up file watchers...
[INFO] Setting up file watchers completed, took 12ms

[INFO] Waiting for all file watchers to be ready...
[INFO] Waiting for all file watchers to be ready completed, took 165ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 68ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 889ms

[INFO] Running build...
[INFO] 1.6s elapsed, 0/1 actions completed.
[INFO] 3.4s elapsed, 0/1 actions completed.
[SEVERE] moor_generator:moor_generator on lib/data/moor_database.dart:
Error running MoorGenerator
NoSuchMethodError: The getter 'typeConverter' was called on null.
Receiver: null
Tried calling: typeConverter
[SEVERE] moor_generator:moor_generator on lib/data/moor_database.dart:
Error running DaoGenerator
NoSuchMethodError: The getter 'typeConverter' was called on null.
Receiver: null
Tried calling: typeConverter
[INFO] Running build completed, took 3.7s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 50ms

[SEVERE] Failed after 3.7s

谷歌搜索后我发现this solution : 但运行 flutter packages pub run build_runner build --delete-conflicting-outputs 后,我收到此错误:

flutter packages pub run build_runner build --delete-conflicting-outputs
[INFO] Generating build script...
[INFO] Generating build script completed, took 350ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 79ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 896ms

[INFO] Running build...
[INFO] Running build completed, took 11ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 47ms

[SEVERE] moor_generator:moor_generator on lib/data/moor_database.dart (cached):
Error running MoorGenerator
NoSuchMethodError: The getter 'typeConverter' was called on null.
Receiver: null
Tried calling: typeConverter
[SEVERE] moor_generator:moor_generator on lib/data/moor_database.dart (cached):
Error running DaoGenerator
NoSuchMethodError: The getter 'typeConverter' was called on null.
Receiver: null
Tried calling: typeConverter
[SEVERE] Failed after 71ms
pub finished with exit code 1

最佳答案

我在它的官方仓库 git 中问了一个问题,我得到了这个答案:

It works if you replace this with integer().autoIncrement()(). The generator should emit a more helpful error message though, I'll take a look at why that didn't happen here.

我改变了:

IntColumn get id =>integer().autoIncrement().call();

至:

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

我的问题消失了。

关于flutter:无法创建 moor_database.g.dart 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039057/

相关文章:

firebase - 在 flutter 中更改个人资料图片和用户名

json - Flutter 将字符串转换为数组

android - 我想在切换屏幕时保持秒表屏幕运行

Flutter Moor 的加入和去向

windows - SQLite 错误参数或其他 API 误用

flutter - 参数类型 'GeneratedIntColumn'不能赋值给参数类型 'int' : Moor-Flutter

构建运行器的 flutter 生成器问题

flutter - 将对象数组拆分为多个列表

dart - 如何在项目构建器中显示来自 Firestore 的列表