3

我正在开发一个 Todo Flutter 应用程序,我正在使用flutter_moor并且bloc我想以最好和最安全的方式实现以下目标:

  • 让用户注册。
  • 让用户在注册后登录。
  • 登录后保存用户 ID 以显示用户待办事项。

实现这一目标的最佳实践是什么?
谢谢大家。
我的 database.dart 如下所示:

import 'package:moor_flutter/moor_flutter.dart';

part 'database.g.dart';

class Todos extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text().withLength(min: 6, max: 32)();
  TextColumn get description => text().named('body')();
  DateTimeColumn get createdAt => dateTime().nullable()();
  BoolColumn get finished => boolean().withDefault(Constant(false))();
  IntColumn get userID => integer()();
  IntColumn get typeID => integer().nullable()();
}

class TodoTypes extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get title => text()();
}

class Users extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get username => text().withLength(min: 3, max: 32)();
  TextColumn get password => text().withLength(min: 3, max: 32)();
}

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

  @override
  int get schemaVersion => 1;

  //Todos table
  Future<List<Todo>> getAllTodos() => select(todos).get();
  Stream<List<Todo>> watchAllTodos() => select(todos).watch();
  Future insertTodo(Todo todo) => into(todos).insert(todo);
  Future updateTodo(Todo todo) => update(todos).replace(todo);
  Future deleteTodo(Todo todo) => delete(todos).delete(todo);
  //Types table
  Future<List<TodoType>> getAllTypes() => select(todoTypes).get();
  Stream<List<TodoType>> watchAllTypes() => select(todoTypes).watch();
  Future insertTodoType(TodoType todoType) => into(todoTypes).insert(todoType);
  Future updateTodoType(TodoType todoType) =>
      update(todoTypes).replace(todoType);
  Future deleteTodoType(TodoType todoType) =>
      delete(todoTypes).delete(todoType);
  //Users table
  Future<List<User>> getAllUsers() => select(users).get();
  Stream<List<User>> watchAllUsers() => select(users).watch();
  Stream<List<User>> getUser(User u) {
    return (select(users)..where((t) => t.username.equals(u.username))).watch();
  }
  Future insertUser(User user) => into(users).insert(user);
  Future updateUser(User user) => update(users).replace(user);
  Future deleteUser(User user) => delete(users).delete(user);
}
4

0 回答 0