Flutter SQflite: How to solve unhandled exception?

时间:2018-03-25 18:46:39

标签: sqlite dart flutter

In my app I do want to save some data with sqflite plugin, but it keeps throwing me the error:

    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

This is my code for regenerating my error:

Rezepte.dart The where I am handling the table named Rezepte from the database.

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 The code for handling the whole database. My Database-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;
  }
}

This two files are the only one, that are able to generate my error. Does anybody have an idea how I could possibly solve the error?

1 个答案:

答案 0 :(得分:0)

Hard to know where the issue happens without a longer stack trace. Make sure you are using the latest version (sqflite >=8.4) especially if you are using --preview-dart-2. I also recommend turning on implicit-casts: false in analysis_options.yaml

analyzer:
  strong-mode:
    implicit-casts: false

Casting a Map to Map<String, dynamic> could throw an error if the map is not of the proper type. The crash reported by Flutter should also indicate the file and line where the issue happened.