gpt4 book ai didi

sqlite - flutter SQflite : How to solve unhandled exception?

转载 作者:IT王子 更新时间:2023-10-29 06:46:22 25 4
gpt4 key购买 nike

在我的应用程序中,我确实想使用 sqflite 插件保存一些数据,但它一直向我抛出错误:

    Unhandled exception: type '_InternalLinkedHashMap' is not a subtype of type 
Map<String, dynamic>' where _InternalLinkedHashMap is from dart:collection

- Map is from dart:core
- String is from dart:core

这是我重新生成错误的代码:

Rezepte.dart 我在其中处理数据库中名为 Rezepte 的表。

class Rezepte{
Rezepte();

int id;
String name;
int personen;
String beschreibung;
int favorit;

static final spalten = ["id", "name", "personen", "beschreibung", "favorit"];

Map toMap(){
Map map = {
"name": name,
"personen": personen,
"beschreibung": beschreibung,
"favorit":favorit
};

if(id != null){
map["id"] = id;
}

return map;
}


static fromMap(Map map){
Rezepte rezepte = new Rezepte();
rezepte.id = map["id"];
rezepte.name = map["name"];
rezepte.personen = map["personen"];
rezepte.beschreibung = map["beschreibung"];
rezepte.favorit = map["favorit"];

return rezepte;
}

}

Datenbank.dart 处理整个数据库的代码。我的数据库-H

import 'dart:async';
import 'dart:io';
import 'package:flutter_app/datenbank/Rezepte.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';

class DatabaseClient{
Database database;

//Datenbank wird erstellt
Future erstellen() async{
Directory pfad = await getApplicationDocumentsDirectory();
String datenbankPfad = join(pfad.path, "rezept_buch.db");

database = await openDatabase(datenbankPfad, version: 1, onCreate: this.erstelleTabellen);
}

//Die Tabellen werden erstellt
Future erstelleTabellen(Database db, int version) async{
await db.execute("""
create table rezept (
id integer primary key,
name text not null,
personen_anzahl integer not null,
beschreibung text default null,
favorit integer default 0
)
""");
}


Future setRezept(Rezepte rezepte) async{
var count = Sqflite.firstIntValue(await database.rawQuery("SELECT * FROM rezept WHERE name =?", [rezepte.name]));
if(count == 0){
rezepte.id = await database.insert("rezept", rezepte.toMap());
} else {
await database.update("rezept", rezepte.toMap(), where: "id = ?", whereArgs: [rezepte.id]);
}

return rezepte;
}

//Daten aus Tabellen holen
Future getAllRezepte(int id) async{
List ergebnisse = await database.query("rezept", columns: Rezepte.spalten, where: "id=?", whereArgs: [id]);
Rezepte rezepte = Rezepte.fromMap(ergebnisse[0]);

return rezepte;
}
}

只有这两个文件能够产生我的错误。有人知道我如何解决错误吗?

最佳答案

如果没有更长的堆栈跟踪,很难知道问题发生在哪里。确保您使用的是最新版本 (sqflite >=8.4),尤其是当您使用 --preview-dart-2 时。 .我还建议打开 implicit-casts: falseanalysis_options.yaml

analyzer:
strong-mode:
implicit-casts: false

类型转换 MapMap<String, dynamic>如果 map 的类型不正确,可能会抛出错误。Flutter 报告的崩溃还应该指出发生问题的文件和行。

关于sqlite - flutter SQflite : How to solve unhandled exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49479589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com