首页 > 解决方案 > 从 Flutter 中的 Instance 获取价值

问题描述

我正在使用提供者模式在列表上发出搜索请求。

List<Device> _devices = [
    Device(one: 'Apple', two: 'iphone'),
    Device(one: 'Samsung', two: 'Galaxy')
];

查询是这样的

List<Device> queryQuery(String value) {
return _devices
    .where((device) => device.one.toLowerCase().contains(value.toLowerCase()))
    .toList();

当我传递值Apple时,我期望得到的结果是iphone

但是当我这样编码时,我得到的屏幕上的结果是[instance of 'Device']

child: Text('${deviceData.getDevice('Apple')}'

我确实知道我应该使用某种键使用两个...但我不知道:-(

标签: flutterdartflutter-provider

解决方案


您序列化了错误的对象。

你所做的最终类似于:

Text(Device(one: 'Apple', two: 'iphone').toString());

但你不想这样做Device.toString()。相反,您想要的是传递Device.two给您的Text.

因此,您的最终结果是:

Text('${chordData.chordExpand('Apple').two}')

推荐阅读