Flutter SQflite:如何解决未处理的异常?
Flutter SQflite: How to solve unhandled exception?
在我的应用程序中,我确实想使用 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 我从数据库中处理 table 名为 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
时。我还建议在 analysis_options.yaml
中打开 implicit-casts: false
analyzer:
strong-mode:
implicit-casts: false
如果地图类型不正确,将 Map
转换为 Map<String, dynamic>
可能会引发错误。
Flutter 报告的崩溃也应该指出发生问题的文件和行。
在我的应用程序中,我确实想使用 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 我从数据库中处理 table 名为 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
时。我还建议在 analysis_options.yaml
implicit-casts: false
analyzer:
strong-mode:
implicit-casts: false
如果地图类型不正确,将 Map
转换为 Map<String, dynamic>
可能会引发错误。
Flutter 报告的崩溃也应该指出发生问题的文件和行。