Flutter-SQflite'_InternalLinkedHashMap <dynamic,dynamic =“”>'不是'Map <string,dynamic =“”>'类型的子类型

时间:2018-09-28 11:12:23

标签: android ios dart flutter

我正在尝试在sqflite数据库中保存一些数据,但仍然出现错误:

[ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled
exception:
    type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'

该错误表明该函数内部有错误:

Future<RecipesDB> insertRecipe(RecipesDB recipe) async{
    var count = Sqflite.firstIntValue(await _db.rawQuery("SELECT COUNT(*) FROM recipes WHERE name = ?", [recipe.name]));
    if(count == 0){
      recipe.id = await _db.insert("recipes", recipe.toMap()); //"toMap()" function returns the error;
    } else {
      await _db.update("recipes", recipe.toMap(), where: "id = ?", whereArgs: [recipe.id]);
    }
    return recipe;
  }

recipe.toMap()指向我的班级:

import 'dart:convert';

class RecipesDB{
  RecipesDB();

  int id, favorite;
  double workDuration;
  String name, definition, timestamp;

  static final columns = ["id", "name", "definition","duration", "favorite", "timestamp"];

  Map toMap(){
    Map map = {
      "name": name,
      "definition": definition,
      "favorite": favorite,
      "duration": workDuration,
      "timestamp": timestamp
    };

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

    return map;
  }

  static fromMap(Map map){
    RecipesDB recipes = new RecipesDB();

    recipes.id = map["id"];
    recipes.name = map["name"];
    recipes.definition = map["definition"];
    recipes.workDuration = map["duration"];
    recipes.favorite = map["favorite"];
    recipes.timestamp = map["timestamp"];

    return recipes;
  }

}

我不知道如何解决此错误。如果你们中的某人可以救我,那将是很棒的。

预先感谢XD!

1 个答案:

答案 0 :(得分:1)

更改

Map toMap(){
  Map map = {

Map<String,dynamic> toMap(){
  Map<String,dynamic> map = {