I am building flutter app with local json file, my json looks like
[
{
"category": "ملابس",
"count": 1,
"description": "ملابس رجالي",
"reference": "",
"color": "أحمر "
}
]
I need the user to find the product when he/she search e.g color, if the user search exactly 'أحمر' its works fine, but if the entered value was 'احمر' the condtion will be false so there is nothing appear in the list.
search function
Future<void> searchJson(query) async {
final jsondata = await rootBundle.loadString('assets/json/product.json');
final list = await json.decode(jsondata) as List<dynamic>;
List data = list
.where((map) => map['color']
.replaceAll(RegExp(String.fromCharCodes(arabicTashkelChar)), "")
.contains(query))
.toList();
setState(() {
items = data;
});
}
and here here is my arabicTashkelChar
list for special Arabic character
const Iterable<int> arabicTashkelChar = [
1617,
124,
1614,
124,
1611,
124,
1615,
124,
1612,
124,
1616,
124,
1613,
124,
1618,
625,
627,
655,
];
I used arabicTashkelChar
to make the string like this 'اللغَةُ الْعَرَبِيَّة' to this 'اللغة العربية'.
I don't have any problem with arabicTashkelChar
its works fine my problem only with Alif (أ إ آ).
Now I want to make string like 'أحمر' to 'احمر' Also 'آ أ إ ' to 'ا' how to achieve this thing?
CodePudding user response:
you need to make some thing like this
void main() {
String myChar = 'أإآ';
print(myChar); // result = أإآ
String newChar = normalise(myChar);
print(newChar ); // result ااا
}
String myNormalise(String input) {
return input
// Replace Alifs with Hamza Above/Below and with Madda Above by Alif
.replaceAll('\u0622', '\u0627')
.replaceAll('\u0623', '\u0627')
.replaceAll('\u0625', '\u0627');
}
Edit
In your setuation your search code will be:
Future<void> searchJson(query) async {
final jsondata = await rootBundle.loadString('assets/json/product.json');
final list = await json.decode(jsondata) as List<dynamic>;
List data = list
.where((map) => map['color']
.replaceAll('\u0622', '\u0627')
.replaceAll('\u0623', '\u0627')
.replaceAll('\u0625', '\u0627')
.replaceAll(RegExp(String.fromCharCodes(arabicTashkelChar)), '')
.contains(query))
.toList();
setState(() {
items = data;
});
}
Another methode add a new element to your json file called search and write waht you want the user search for, so the json will some thing like:
[
{
"category": "ملابس",
"count": 1,
"description": "ملابس رجالي",
"reference": "",
"color": "أحمر ",
"search":"احمر"
}
]
And then change
...
List data = list.where((map) => map['search'].contains(query)).toList();
...
This is the answer for you question.