微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以在颤振中显示 TextFormField 中的外键数据

如何解决是否可以在颤振中显示 TextFormField 中的外键数据

我是 Flutter 的新手,想创建 Category 表,其中外键是同一个表的 id 来创建子类别。我不知道如何在颤振中使用此类表创建数据。下面是我的分类模型类代码和databaseHelper类代码

类别型号代码

final String tableCategory = 'category';

class CategoryModel {
  final int? id;
  final String name;
  final String desc;
  final int? parentId;
  final bool isActive;
  final DateTime createdTime;
  final String createdBy;

  CategoryModel(
      {this.id,required this.name,required this.desc,this.parentId,required this.isActive,required this.createdTime,required this.createdBy});

  factory CategoryModel.fromMap(Map<String,dynamic> json) => CategoryModel(
        id: json["id"],name: json["name"],desc: json["desc"],parentId: json["parentId"],isActive: json["isActive"] == 1,createdTime: DateTime.parse(json["createdTime"]),createdBy: json["createdBy"],);

  Map<String,dynamic> toMap() => {
        "id": id,"name": name,"desc": desc,"parentId": parentId,"isActive": isActive ? 1 : 0,"createdTime": createdTime.toIso8601String(),"createdBy": createdBy,};
}

databaseHelper 类代码

import 'package:budget_app/model/categoryModel.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class DB {
  static final DB instance = DB._init();

  static Database? _database;

  DB._init();

  Future<Database> get database async{
    if(_database != null) return _database!;

    _database = await _initDB('budget.db');
    return _database!;
  }

  Future<Database> _initDB(String filePath) async {
    final dbPath = await getDatabasesPath();
    final path = join(dbPath,filePath);
    
    return await openDatabase(path,version: 1,onCreate: _onCreate,onConfigure: _onConfigure);
  }

  Future _onCreate(Database db,int version) async {
      await db.execute('''
        CREATE TABLE $tableCategory(
          id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NOT NULL,desc TEXT NOT NULL,parentId INTEGER,FOREIGN KEY(parentId) REFERENCES $tableCategory (id) ON DELETE NO ACTION ON UPDATE NO ACTION,isActive BOOLEAN NOT NULL,createdTime DATETIME DEFAULT (cast(strftime('%s','Now') as int)),createdBy TEXT NOT NULL,)
      ''');
    }

  Future _onConfigure(Database db) async {
      // Add support for cascade delete
      await db.execute("PRAGMA foreign_keys = ON");
    }

  //Custom Category Functions
  Future<bool> insertCategory(CategoryModel categoryModel) async {
    final db = await instance.database;
    db.insert("Categories",categoryModel.toMap());
    return true;
  }

  Future<List<CategoryModel>> getCategory() async {
    final db = await instance.database;
    final List<Map<String,Object?>> categories = await db.query("Categories");
    return categories.map((e) => CategoryModel.fromMap(e)).toList();
  }

  Future<int> updateCategory(CategoryModel categoryModel) async {
    final db = await instance.database;

    return db.update(
      tableCategory,categoryModel.toMap(),where: 'id = ?',whereArgs: [categoryModel.id],);
  }

  Future<int> deleteCategory(int id) async {
    final db = await instance.database;

    return await db.delete(
      tableCategory,whereArgs: [id],);
  }

  Future close() async {
    final db = await instance.database;

    db.close();
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。