颤振 - 从 CSV 数据创建搜索委托数据'getter'模型



目前,我正在构建一个Flutter应用程序来尝试和发展我的技能,其中包括一个搜索委托功能。目前,我的搜索委托工作得很好,当数据从json格式导入时,它看起来像这样:

[
{
name: John,
age: 22,
height: 1.85,
searchTerm: John 22 1.85,
},
{
name: Alice,
age: 24,
height: 1.90,
searchTerm: Alice 24 1.90,
},
{
name: Bruce,
age: 35,
height: 1.76,
searchTerm: Bruce 35 1.76,
}
]

为了允许我的搜索委托工作,我建立了一个getter模型,如下所示:

import 'dart:convert';
import 'package:flutter/services.dart';
class People {
final String name;
final double age;
final double height;
final String searchTerm;
People({
required this.name,
required this.age,
required this.height,
required this.searchTerm,
});
static People fromJson(Map<String, dynamic> json) => People(
name: json['name'],
age: json['age'],
height: json['height'],
searchTerm: json['searchTerm'],

);
}
class PeopleGetter {
static Future<List<People>> getPeopleSuggestions(String query) async {
try {
final String response =
await rootBundle.loadString('assets/data/people.json');
final List people = json.decode(response);
return people
.map((json) => People.fromJson(json))
.where((people) {
final searchTermLower = people.searchTerm.toLowerCase();
final queryLower = query.toLowerCase();
return searchTermLower.contains(queryLower);
}).toList();
} catch (e) {
print(e);
}
throw '';
}
}

这在我的搜索委托中确实工作得很好。但是,由于将数据存储在CSV文件中可以显着减少总体数据大小,因此总体应用程序大小,因此我想用CSV数据替换json数据。我已经尝试修改我的'getter'函数从json数据,使用CSV数据(使用CSV包,从pub.dev),你可以在这里看到:

import 'package:csv/csv.dart';
import 'package:flutter/services.dart';
class People {
final String name;
final double age;
final double height;
final String searchTerm;
People({
required this.name,
required this.age,
required this.height,
required this.searchTerm,
});
static People fromCSV(Map<String, dynamic> csv) => People(
name: csv['name'],
age: csv['age'],
height: csv['height'],
searchTerm: csv['searchTerm'],

);
}
class PeopleGetter {
static Future<List<People>> getPeopleSuggestions(String query) async {
try {
final String response =
await rootBundle.loadString('assets/data/people.csv');
final List people = CsvToListConverter(response);
return people
.map((csv) => People.fromCSV(csv))
.where((people) {
final searchTermLower = people.searchTerm.toLowerCase();
final queryLower = query.toLowerCase();
return searchTermLower.contains(queryLower);
}).toList();
} catch (e) {
print(e);
}
throw '';
}
}

不幸的是,当我在搜索委托中使用这个版本时,我得到了错误:

'type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'.

我觉得我只是在某个地方犯了一个非常简单的错误,但现在,我就是找不到它。我将非常感谢任何帮助纠正我的代码,让我搜索与CSV数据,而不是JSON数据。谢谢!

您的数据类型似乎不再对应:

提供List<dynamic>给需要Map<>的函数

people.map((csv) => People.fromCSV(csv))

static People fromCSV(Map<String, dynamic> csv) => People(
name: csv['name'],
age: csv['age'],
height: csv['height'],
searchTerm: csv['searchTerm'],
);

你能试着从CSV重写你的函数来适应新的传入数据吗?我不确定你得到的数据是什么但也许是这样的?

static People fromCSV(List<dynamic> csv) => People(
name: csv[0] ?? "",
age: csv[1] ?? -1,
height: csv[2] ?? -1,
searchTerm: csv[3] ?? "",
);

编辑(修复几个剩余的bug)

  • CSV包更新版本
  • 正确地从csv
  • 中检索double
  • 设置分隔符参数以分隔CSV
  • 中的两个人。
class People {
final String name;
final double age;
final double height;
final String searchTerm;
People({
required this.name,
required this.age,
required this.height,
required this.searchTerm,
});
static People fromCSV(List<dynamic> csv) => People(
name: csv[0] ?? '',
age: double.parse(csv[1].toString()),
height: double.parse(csv[2].toString()),
searchTerm: csv[3] ?? '',
);
}
class PeopleGetter {
static Future<List<People>> getPeopleSuggestions(String query) async {
try {
final String response = await rootBundle.loadString('assets/data.csv');
final List<List<dynamic>> people =
CsvToListConverter().convert(response, eol: "n");
return people.map((csv) => People.fromCSV(csv)).where((people) {
final searchTermLower = people.searchTerm.toLowerCase();
final queryLower = query.toLowerCase();
return searchTermLower.contains(queryLower);
}).toList();
} catch (e) {
print(e);
}
throw '';
}
}

相关内容

  • 没有找到相关文章

最新更新