概述
SearchHelper 是由 DOTCODER 开发的用于搜索功能的代码封装。它是一种帮助开发人员轻松执行搜索的工具。
工作原理
SearchHelper 是一个类,它具有 三个 静态方法。
1- wordSearch()
2- stringInstantSearch()
3- searchModel()
第一个和第二个方法需要传入相同的 参数。
如下所示。
| 参数 | 描述 |
|---|---|
| data | 此参数接受 List<Map<String,dynamic>> 作为数据源,并在此数据上执行搜索。 |
| 键 | 此参数接受 List<String> 作为搜索字段,例如在标题、描述字段中进行搜索。 |
| searchWord | 此参数基本上是需要在数据中搜索的单词。 |
笔记
SearchWord 参数接受动态值,例如它接受 bool, int, double 和 String。
wordSearch() 和 stringInstantSearch() 如何工作
它以 List<Map<String,dynamic>> 作为数据参数,以 List<String> 作为属性参数,searchWord 参数接受动态值。它返回 List<Map<String,dynamic>> 作为搜索结果。
searchModel() 如何工作
它以 List<T> 作为数据参数,以 List<String> 作为键参数,以 Map<String,String> 作为要在每个用户定义的模型中搜索的对象,searchWord 参数接受动态值。它返回 List<Map<String,dynamic>> 作为搜索结果。
重要提示:
请确保您使用的每个模型都有其 toJson() 和 fromJson() 方法,因为插件在其底层工作中使用它们。
| 参数 | 描述 |
|---|---|
| data | 此参数接受 List<T> 作为数据源,并在此数据上执行搜索。 |
| 属性 | 此参数接受 List<String> 作为搜索字段,例如在标题、描述字段中进行搜索。 |
| innerObject | 如果您想在 List<T> 中搜索,并且 T 中有另一个对象,那么您可以使用 innerObject 来搜索包含该模型的特定对象。 |
| searchWord | 此参数基本上是需要在数据中搜索的单词。 |
入门
在 pubspec.yaml 中
dependencies:
search_helper: ^0.0.7
第一个和第二个方法的代码示例,因为它们是相同的
import 'package:search_helper/search_helper.dart';
void main(){
List<Map<String,dynamic>> items = [
{
'name': "Osama",
'age': 21,
'father': "Noushad"
},
{
'name': "Haseeb",
'age': 20,
'father': "Noushad"
},
{
'name': "Shahrok",
'age': 22,
'father': "Noushad"
},
{
'name': "Asad",
'age': 23,
'father': "Noushad"
},
];
List<String> keys = ['age','name','father'];
List<Map<String,dynamic>> result = SearchHelper.stringInstantSearch(data: items,keys: keys,searchWord: 'osama');
}
基于模型的搜索代码示例
第一个示例
首先,让我们定义我们的 User 模型。我们将使用该 User 模型来搜索一些用户。
class User {
String name;
int age;
User(this.name, this.age);
Map toJson() => {
'name': name,
'age': age,
};
factory User.fromJson(dynamic json) {
return User(json['name'] as String, json['age'] as int);
}
}
如上所述,在搜索模型中需要 toJson() 和 fromJson()。
让我们搜索 List<User>。
import 'package:search_helper/search_helper.dart';
import 'dart:convert';
void main(){
List<User> users = [
User('Osama',12),
User('Haseeb',13),
User("Shahrokh",14)
];
List<User> filteredUsers = [];
var result = SearchHelper.searchModel(
data: users,
properties: ['name',],
searchWord: 'osama'
);
result.forEach((e){
User u = User.fromJson(e);
filteredUsers.add(u);
});
}
第二个示例
让我们对另一个包含教程信息及其作者信息的模型进行一些深入搜索。
所以,让我们先创建 Tutorial 模型的类。
class Tutorial {
String title;
String description;
User? author;
Tutorial(this.title, this.description, {this.author});
Map toJson() {
Map? author =
this.author! != null ? this.author!.toJson() : null;
return {
'title': title,
'description': description,
'author': author,
};
}
factory Tutorial.fromJson(dynamic json) {
return Tutorial(
json['title'] as String,
json['description'] as String,
author:
User.fromJson(json['author'])
);
}
}
现在我们的 Tutorial 模型已完成,让我们使用上面使用的相同的 User 类。
让我们搜索所有作者名字以 osama 开头的教程。
import 'package:search_helper/search_helper.dart';
import 'dart:convert';
void main(){
List<Tutorial> tutorials = [
Tutorial('C++', "This is c++ course.",author: User('Osama', 12)),
Tutorial('C#', "This is c# course.",author: User('Haseeb', 13)),
];
List<Tutorial> filteredTutorials = [];
var result = SearchHelper.searchModel(
data: tutorials,
innerObject: {'author': 'name'},
searchWord: 'osama'
);
result.forEach((e){
Tutorial t = Tutorial.fromJson(e);
filteredTutorials.add(t);
});
}
我们最终从 searchModel() 获得的结果是 List<dynamic>,然后您需要再次将其转换为模型。