有没有办法在使用 flutter 时排除某些字符 list.where
Is there a way to exclude certain characters when using flutter list.where
我有一个 searchDelegate,当用户搜索时,我使用 list.where 进行相应的获取,现在我已经记下了,我有一个问题,当用户不包含一些字符,如逗号或撇号或连字符,它不显示那些结果,
例子
我的清单中有一本书,标题为 => '朱丽叶,我想要的是你。
当用户搜索时没有撇号,甚至错过了逗号,它不会显示那本书,除非你一个字符一个字符地输入它,我想要一种情况,我可以让 where() method
忽略用户的如果你明白我的无知......这是一些代码片段。
import 'package:flutter/material.dart';
import 'book.dart';
import 'book_brain.dart';
class MyDelegate extends SearchDelegate<Book> {
@override
Widget? buildLeading(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
);
}
@override
List<Widget>? buildActions(BuildContext context) {
return [
IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
query.isEmpty ? Navigator.pop(context) : query = '';
},
),
];
}
@override
Widget buildResults(BuildContext context) {
return const Center();
}
@override
Widget buildSuggestions(BuildContext context) {
List<Book> suggestions = BookBrain().allBooks().where((book) {
final result = book.bookText.toLowerCase();
final input = query.toLowerCase();
final result2 = book.bookTitle.toLowerCase();
//here I wanna make it ignore when user misses certain characters
return result.contains(input) || result2.contains(input);
}).toList();
return ListView.builder(
itemCount: suggestions.length,
itemBuilder: (context, suggestIndex) {
final suggestion = suggestions[suggestIndex];
return ListTile(
title: Text(suggestion.bookTitle),
trailing: Text('${suggestion.authorName} ${suggestion.bookYear}',
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.w300,
),
),
onTap: () {
close(context, suggestion);
},
);
});
}
}
您可以使用正则表达式完成此操作。 result
和 result2
将删除所有空格和特殊字符。
// "It's in 4 hours." -> "itsin4hours"
final result = book.bookText.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
final result2 = book.bookTitle.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
更详细的例子here
我有一个 searchDelegate,当用户搜索时,我使用 list.where 进行相应的获取,现在我已经记下了,我有一个问题,当用户不包含一些字符,如逗号或撇号或连字符,它不显示那些结果, 例子 我的清单中有一本书,标题为 => '朱丽叶,我想要的是你。
当用户搜索时没有撇号,甚至错过了逗号,它不会显示那本书,除非你一个字符一个字符地输入它,我想要一种情况,我可以让 where() method
忽略用户的如果你明白我的无知......这是一些代码片段。
import 'package:flutter/material.dart';
import 'book.dart';
import 'book_brain.dart';
class MyDelegate extends SearchDelegate<Book> {
@override
Widget? buildLeading(BuildContext context) {
return IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
);
}
@override
List<Widget>? buildActions(BuildContext context) {
return [
IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
query.isEmpty ? Navigator.pop(context) : query = '';
},
),
];
}
@override
Widget buildResults(BuildContext context) {
return const Center();
}
@override
Widget buildSuggestions(BuildContext context) {
List<Book> suggestions = BookBrain().allBooks().where((book) {
final result = book.bookText.toLowerCase();
final input = query.toLowerCase();
final result2 = book.bookTitle.toLowerCase();
//here I wanna make it ignore when user misses certain characters
return result.contains(input) || result2.contains(input);
}).toList();
return ListView.builder(
itemCount: suggestions.length,
itemBuilder: (context, suggestIndex) {
final suggestion = suggestions[suggestIndex];
return ListTile(
title: Text(suggestion.bookTitle),
trailing: Text('${suggestion.authorName} ${suggestion.bookYear}',
style: const TextStyle(
fontStyle: FontStyle.italic,
fontWeight: FontWeight.w300,
),
),
onTap: () {
close(context, suggestion);
},
);
});
}
}
您可以使用正则表达式完成此操作。 result
和 result2
将删除所有空格和特殊字符。
// "It's in 4 hours." -> "itsin4hours"
final result = book.bookText.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
final result2 = book.bookTitle.replaceAll(RegExp('[^A-Za-z0-9]'), '').toLowerCase();
更详细的例子here