【问题标题】:Flutter - Sembast Database insert List of ObjectsFlutter - Sembast 数据库插入对象列表
【发布时间】:2019-09-29 15:01:07
【问题描述】:

我即将在 Flutter 中使用数据库“Sembast”。 具有字符串和 int 等数据类型的简单对象可以正常工作。但是,在使用 Lists 时会出现问题。

我创建了一个示例,并针对以下教程进行了指导:https://resocoder.com/2019/04/06/flutter-nosql-database-sembast-tutorial-w-bloc/ 在我的示例中,有水果和叶子作为对象。一个水果包含一个叶子列表。

class Fruit {
  final String id;
  final String name;
  final bool isSweet;
  final List<Leaves> leaves;
...
}

class Leaves {
  final String id;
  final String name;
...
}

//Create a sample object
var leaveOne = Leaves(id: "1", name: "leaveOne");
var leaveTwo = Leaves(id: "2", name: "leaveTwo");
var leaveThree = Leaves(id: "3", name: "leaveThree");

var leavesList = List<Leaves>();
leavesList.add(leaveOne);
leavesList.add(leaveTwo);
leavesList.add(leaveThree);

var fruit = Fruit(id: "1", name: "Apple", isSweet: true, leaves: leavesList);
_fruitDao.insert(fruit);


// The fruitDao.insert makes following
Future insert(Fruit fruit) async {
  await _fruitStore.add(await _db, fruit.toJson());
}

JSON 看起来像这样:{id: 1, name: Apple, isSweet: true, Leaves: [Instance of 'Leaves', Instance of 'Leaves', Instance of 'Leaves']}

错误如下: [错误:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:无效的参数:不支持的“叶子”类型叶子的值实例

【问题讨论】:

    标签: database dart flutter flutter-dependencies


    【解决方案1】:

    除了@alextk 回答之外,这是一个示例,在没有任何代码生成或库的情况下进行转换。

    class Fruit {
      final String id;
      final String name;
      final bool isSweet;
      final List<Leaves> leaves;
    
      Fruit({this.id, this.name, this.isSweet, this.leaves});
    
      Map<String, dynamic> toMap() {
        return {
          'id': id,
          'name': name,
          'isSweet': isSweet,
          'leaves': leaves.map((leave) => leave.toMap()).toList(growable: false)
        };
      }
    
      static Fruit fromMap(Map<String, dynamic> map) {
        return Fruit(
          id: map['id'],
          name: map['name'],
          isSweet: map['isSweet'],
          leaves: map['leaves'].map((mapping) => Leaves.fromMap(mapping)).toList().cast<Leaves>(),
        );
      }
    }
    
    class Leaves {
      final String id;
      final String name;
    
      Leaves({this.id, this.name});
    
      Map<String, dynamic> toMap() {
        return {
          'id': id,
          'name': name,
        };
      }
    
      static Leaves fromMap(Map<String, dynamic> map) {
        return Leaves(
          id: map['id'],
          name: map['name'],
        );
      }
    }
    

    【讨论】:

    • 不使用静态 fromMap 方法,你可以很好地使用工厂方法
    【解决方案2】:

    正如所指出的,Instance of 'Leaves' 不是有效类型,因此每个Leave 也必须进行转换。如果没有看到你的toJson(),很难猜出你在做什么,但这样的事情应该可以工作(可以在很大程度上进行优化):

    class Fruit {
      final String id;
      final String name;
      final bool isSweet;
      final List<Leaves> leaves;
    
      Fruit({this.id, this.name, this.isSweet, this.leaves});
    
      Map<String, dynamic> toJson() => <String, dynamic>{
            'id': id,
            'name': name,
            'isSweet': isSweet,
            'leaves': leaves?.map((leave) => leave.toJson())?.toList(growable: false)
          };
    }
    
    class Leaves {
      final String id;
      final String name;
    
      Leaves({this.id, this.name});
    
      Map<String, dynamic> toJson() => <String, dynamic>{'id': id, 'name': name};
    }
    

    你的 json 应该看起来像这样:

    {
      "id": "1",
      "name": "Apple",
      "isSweet": true,
      "leaves": [
        {
          "id": "1",
          "name": "leaveOne"
        },
        {
          "id": "2",
          "name": "leaveTwo"
        },
        {
          "id": "3",
          "name": "leaveThree"
        }
      ]
    }
    

    【讨论】:

    • 感谢您的回复!我正在使用“json_serializable” - 包。所以这会生成函数toJson()fromJson() 的代码。如果我编辑这个生成的文件(通常你不应该这样做),并粘贴你的代码来映​​射叶子('leaves': leaves?.map((leave) =&gt; leave.toJson())?.toList(growable: false)) - 它工作正常。
    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 2020-08-29
    • 2019-11-20
    • 2020-02-10
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2020-01-28
    相关资源
    最近更新 更多