Listview поиск не может найти данные в списке
Я пытаюсь добавить поиск по имени для символов из SW Api, но поиск по какой-то причине не работает, Может ли кто-нибудь сказать мне, где ошибка? Может быть, проблема в самом списке?
main
class _SWMainState extends State<SWMain> {
Icon customIcon = Icon(Icons.search);
static Text titleText = Text("Star Wars API");
Widget customSearchBar = titleText;
var search = List<Character>();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: customSearchBar,
actions: <Widget>[
IconButton(
icon: customIcon,
onPressed: () {
setState(() {
if (this.customIcon.icon == Icons.search) {
this.customIcon = Icon(Icons.cancel);
this.customSearchBar = TextField(
textInputAction: TextInputAction.go,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Search character"),
onChanged: (text) {
text = text.toLowerCase();
search = search.where((search) {
var charTitle = search.name.toLowerCase();
return charTitle.contains(text);
}).toList();
},
style: TextStyle(color: Colors.white, fontSize: 16),
);
} else {
this.customIcon = Icon(Icons.search);
this.customSearchBar = titleText;
}
});
})
],
backgroundColor: Colors.amber,
),
body: FutureBuilder<List<Character>>(
future: fetchCharacters(http.Client()),
builder: (context, snapshot) {
if (snapshot.hasError) print(snapshot.error);
return snapshot.hasData
? CharacterList(character: snapshot.data)
: Center(child: CircularProgressIndicator());
},
),
),
);
}
}
characterList
import 'package:flutter/material.dart';
import 'characterModel.dart';
import 'secondPage.dart';
class CharacterList extends StatelessWidget {
final List<Character> character;
CharacterList({Key key, this.character}) : super(key: key);
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: character.length,
itemBuilder: (context, index) {
return Card(
elevation: 5,
margin: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
child: Container(
padding: EdgeInsets.all(15),
child: ListTile(
title: Text(
character[index].name,
style: TextStyle(fontSize: 18, color: Colors.black),
),
onTap: () {
print('Print character $character');
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(),
settings: RouteSettings(arguments: character[index])));
},
),
),
);
},
);
}
}