如何使用moor_flutter软件包集成数据库?

时间:2019-12-20 09:58:39

标签: database sqlite flutter dart flutter-moor

我的Flutter应用程序中有一个设置页面,我正在使用moor_flutter。在我的pubspec.yaml中,我的依赖项中有moor_flutter ^ 2.0.0,而我的dev_dependencies中有moor_generator:^ 2.0.0。

我创建了我的数据库类(见下文),并运行了通常的“ flutter pub run build_runner build ”命令 database.dart

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

part 'database.g.dart';

// Map Parameters table definition
class Parameters extends Table {
  BoolColumn get tilt => boolean().withDefault(Constant(true))();
  BoolColumn get traffic => boolean().withDefault(Constant(false))();
  BoolColumn get compass => boolean().withDefault(Constant(true))();
  BoolColumn get rotate => boolean().withDefault(Constant(true))();
  BoolColumn get zoom => boolean().withDefault(Constant(true))();
  /*TextColumn get type => text().named('normal')();
  TextColumn get area => text().named('Uni Campus')();*/
}

// Theme table definition
class Themes extends Table {
  TextColumn get timing => text().named('auto')();
  TextColumn get dayTheme => text().named('standard')();
  TextColumn get nightTheme => text().named('night')();
}

class Places extends Table {
  IntColumn get id => integer().autoIncrement()();
  BoolColumn get isFavourite => boolean().withDefault(Constant(false))();
  TextColumn get placeName => text().withLength(min: 1, max: 30)();
  TextColumn get placeLocation => text().withLength(min: 1, max: 60)();
  DateTimeColumn get dateAdded => dateTime().nullable()();
}

// App Database Class
@UseMoor(tables: [Parameters, Themes, Places])
class AppDatabase extends _$AppDatabase {
  AppDatabase()
      : super(FlutterQueryExecutor.inDatabaseFolder(
            path: 'db.sqlite', logStatements: true));

  // Database Schema Version
  @override
  int get schemaVersion => 1; // Change this with every DB change

  // Queries
  // Map parameters queries
  Future<List<Parameter>> getAllParameters() => select(parameters).get();
  Stream<List<Parameter>> watchAllParameters() => select(parameters).watch();
  Future updateParameter(Parameter parameter) =>
      update(parameters).replace(parameter);

  // Theme settings
  Future<List<Theme>> getAllThemes() => select(themes).get();
  Stream<List<Theme>> watchAllThemes() => select(themes).watch();
  Future updateTheme(Theme theme) => update(themes).replace(theme);

  // User's favourite places
  Future<List<Place>> getAllPlaces() => select(places).get();
  Stream<List<Place>> watchAllPlaces() => select(places).watch();
  Future insertPlace(Place place) => into(places).insert(place);
  Future updatePlace(Place place) => update(places).replace(place);
  Future deletePlace(Place place) => delete(places).delete(place);
}

在我的setting_form.dart中,

@override
  Widget build(BuildContext context) {
    final AppDatabase db = Provider.of<AppDatabase>(context);
    return Provider(
      create: (_) => AppDatabase(),
      child: MaterialApp(
        home: Scaffold(
          body: StreamBuilder(
            stream: db.watchAllParameters(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              return Container(
                child: .....

以下是我收到的错误。任何帮助将不胜感激。

  

执行热重启...
  在1,740毫秒内重新启动应用程序。 I /颤振(16100):══╡例外   小工具图书馆的考查   ╞═════════════════════════════════════════════════ ══════════我/扑   (16100):最终确定时引发了以下NoSuchMethodError   小部件树:I / flutter(16100):调用了方法'cancel'   空值。 I / flutter(16100):接收方:空I / flutter(16100):尝试过   调用:cancel()I / flutter(16100):I / flutter(16100):当   引发异常,这是堆栈:I / flutter(16100):#0
  Object.noSuchMethod(dart:core-patch / object_patch.dart:53:5)I / flutter   (16100):#1 _TheMapState.dispose   (包裹:manchester_maps / screens / campus_map / campus_map.dart:157:24)   I /颤振(16100):#2 StatefulElement.unmount   (包:flutter / src / widgets / framework.dart:4435:12)I / flutter   (16100):#3 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1748:13)I / flutter   (16100):#4 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

5 ComponentElement.visitChildren(package:flutter / src / widgets / framework.dart:4272:14)I / flutter

     

(16100):#6 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#7 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

8 SingleChildRenderObjectElement.visitChildren(package:flutter / src / widgets / framework.dart:5433:14)I / flutter

     

(16100):#9 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#10 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

11 SingleChildRenderObjectElement.visitChildren(package:flutter / src / widgets / framework.dart:5433:14)I / flutter

     

(16100):#12 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#13 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

14 ComponentElement.visitChildren(package:flutter / src / widgets / framework.dart:4272:14)I / flutter

     

(16100):#15 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#16 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

17 ComponentElement.visitChildren(package:flutter / src / widgets / framework.dart:4272:14)I / flutter

     

(16100):#18 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#19 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

20 ComponentElement.visitChildren(package:flutter / src / widgets / framework.dart:4272:14)I / flutter

     

(16100):#21 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#22 _InactiveElements._unmount。   (package:flutter / src / widgets / framework.dart:1746:7)I / flutter(16100):

     

23 ComponentElement.visitChildren(package:flutter / src / widgets / framework.dart:4272:14)I / flutter

     

(16100):#24 _InactiveElements._unmount   (包:flutter / src / widgets / framework.dart:1744:13)I / flutter   (16100):#25 ListIterable.forEach   (dart:_internal / iterable.dart:41:13)I / flutter(16100):#26
  _InactiveElements._unmountAll(package:flutter / src / widgets / framework.dart:1757:25)I / flutter   (16100):#27 BuildOwner.finalizeTree。   (包:flutter / src / widgets / framework.dart:2519:27)I / flutter   (16100):#28 BuildOwner.lockState   (包:flutter / src / widgets / framework.dart:2350:15)I / flutter   (16100):#29 BuildOwner.finalizeTree   (package:flutter / src / widgets / framework.dart:2518:7)I / flutter(16100):

     

30 WidgetsBinding.drawFrame(package:flutter / src / widgets / binding.dart:775:18)I / flutter(16100):

     

31 RendererBinding._handlePersistentFrameCallback(包:flutter / src / rendering / binding.dart:283:5)I / flutter(16100):

     

32 SchedulerBinding._invokeFrameCallback(程序包:flutter / src / scheduler / binding.dart:1102:15)I / flutter

     

(16100):#33 SchedulerBinding.handleDrawFrame   (package:flutter / src / scheduler / binding.dart:1041:9)I / flutter(16100):

     

34 SchedulerBinding._handleDrawFrame(package:flutter / src / scheduler / binding.dart:957:5)I / flutter(16100):

     

38 _invoke(dart:ui / hooks.dart:259:10)I / flutter(16100):#39 _drawFrame(dart:ui / hooks.dart:217:3)I / flutter(16100):(已消除来自dart包的3帧:异步)I / flutter(16100):

     

═══════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══   I / flutter(16100):引发了另一个异常:错误:找不到   此SettingsForm窗口小部件上方的正确提供者

此外,我不知道需要在SwitchListTile中输入什么作为值。

SwitchListTile(
    value: true,
    onChanged: (bool value) {
    setState(() {
        _allowTilt = value;
        print("ALLOW TILT => $_allowTilt");
        //db.updateParameter(_allowTilt);
        });
    },
    title: Text(AppLocalizations.of(context)
           .settingsLabelTilt),
          activeColor: Theme.UniColour.primary[900],
    ),
    SwitchListTile(
        value: _enableRotate,
            Changed: (bool value) {
            ...
            ...
            ...

谢谢

0 个答案:

没有答案