Flutter SQFlite包中的“方法连接未定义”错误和Flutter SQLite中的“命名参数'id'未定义”错误

cgyqldqp  于 2023-05-29  发布在  SQLite
关注(0)|答案(1)|浏览(147)

我正在使用SQLite存储一个flutter应用程序。我使用SQflite package来使用SQLite。下面是我编写的所有CRUD函数的文件:(我已经包括了注解,以指出文件中的错误在哪里)

import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'Employee.dart';

class DBmanager {
  late Database _database;

  Future openDb() async {
    if (_database == null) {
      _database = await openDatabase(
          join(await getDatabasesPath(), 'medicine.db'),//getting an error in this line for the join keyword
          version: 1, onCreate: (Database db, int version) async {
        await db.execute(
          'CREATE TABLE Employee(id INTEGER PRIMARY KEY autoincrement, medicineName TEXT, Frequency TEXT, dosageAmount TEXT, medicineType TEXT, instructions TEXT)',
        );
      });
    }
  }

  //function to insert medicine in the database
  Future<int> insertMedicine(Medicine medicine) async {
    await openDb();
    return await _database.insert('Medicine', medicine.toMap());
  }

  //function to get information of medicine from database
  Future<List<Medicine>> getMedicineList() async {
    await openDb();
    final List<Map<String, dynamic>> maps = await _database.query('Medicine');
    return List.generate(maps.length, (i) {
      return Medicine(
        id: maps[i]['id'],//getting error in this line for 'id'
        medicineName: maps[i]['Medicine name'],
        frequency: maps[i]['Frequency'],
        dosageAmount: maps[i]['Dosage Amount'],
        medicineType: maps[i]['Medicine Type'],
        instructions: maps[i]['Instructions'],
      );
    });
  }

  //function to update the information of medicine in the database
  Future<int> updateMedicine(Medicine medicine) async {
    await openDb();
    return await _database.update('Medicine', medicine.toMap(),
        where: "id = ?", whereArgs: [medicine.id]);
  }

  //function to delete medicine from database
  Future<void> deleteMedicine(int id) async {
    await openDb();
    await _database.delete('Medicine', where: "id = ?", whereArgs: [id]);
  }
}

在上面的代码中,我得到了以下错误:-第12行:未为类型“DBmanager”定义方法“join”。尝试将名称更正为现有方法的名称或定义名为“join”的方法。
第37行:未定义命名参数'id'。尝试将名称更正为现有命名参数的名称,或使用名称“id”定义命名参数。
下面是我创建医学类的文件:

import 'package:flutter/material.dart';

class Medicine {
  int id;
  String medicineName;
  String frequency;
  String dosageAmount;
  String medicineType;
  String instructions;

  Medicine({
    this.id,//getting an error in this line for id
    required this.medicineName,
    required this.frequency,
    required this.dosageAmount,
    required this.medicineType,
    required this.instructions,
  });

  Map<String, dynamic> toMap() {
    return {
      'Medicine name': medicineName,
      'Frequency': frequency,
      'dosage Amount': dosageAmount,
      'Medicine Type': medicineType,
      'Instructions': instructions
    };
  }
}

在上面的代码中,我得到了以下错误:-第24行:参数“id”不能有“null”值,因为它的类型,但隐式默认值为“null”。尝试添加一个显式的非'null'默认值或'required'修饰符。
我不能使用'required'修饰符,因为id的值不会由用户给出,它会自动递增。我也尝试初始化'id',但没有解决错误。
救命啊!

o75abkj4

o75abkj41#

需要导入path来获取join方法。

import 'package:path/path.dart';

相关问题