创建了一个列表元素搜索。我从网上找的名单。数据全部显示,但是当我想使用_runFilter
方法时,数据消失,什么也没有发生。1。如何使数据搜索工作?2。当我打开页面,我的_foundAddressesInStationList是空的,但我初始化它和数据应该显示,为什么它不显示?提前谢谢。
List<PublicChargingStationModel> stationsList = [];
List<PublicChargingStationModel> _foundAddressesInStationList = [];
@override
void initState() {
_loadStations();
_foundAddressesInStationList = stationsList;
super.initState();
}
void _runFilter(String enteredKeyword) {
List<PublicChargingStationModel> resultAddressesInStationList = [];
if (enteredKeyword.isEmpty) {
resultAddressesInStationList = stationsList;
} else {
resultAddressesInStationList = stationsList
.where((element) => element.address!
.toLowerCase()
.contains(enteredKeyword.toLowerCase()))
.toList();
}
setState(
() {
_foundAddressesInStationList = resultAddressesInStationList;
},
);
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final double paddingTop = MediaQuery.of(context).padding.top;
return Container(
width: size.width,
height: size.height,
child: _child(size, paddingTop),
);
}
void _loadStations() async {
final StationCubit stationCubit = BlocProvider.of<StationCubit>(context);
stationsList = await stationCubit.getPublicChargingStation();
}
}
在init方法中,您正在调用异步的cubit,但没有等待结果,您使用空的stationList初始化_foundAddressesInStationList。因此,像这样在load方法中初始化_foundAddressesInStationList。
void _loadStations() async {
final StationCubit stationCubit = BlocProvider.of<StationCubit>(context);
stationsList = await stationCubit.getPublicChargingStation();
_foundAddressesInStationList = stationsList;
setState((){});
}
- 过滤器应该工作良好。你有任何警告或错误吗?你有预期的值在resultAddressesInStationList而过滤?
编辑:过滤器的替代方法:
stationsList.forEach((element) {
if(element.address!.toLowerCase().contains(enteredKeyword.toLowerCase())) {
resultAddressesInStationList.add(element);
}
});
_foundAddressesInStationList = resultAddressesInStationList;
setState(() {});
您添加空检查操作符的唯一地方是element.address!对空值使用的空检查操作符&;表示地址应该为空或不存在。
也试试element["address"]
编辑2:
你的搜索小部件应该是:
class MySearchWidget extends StatefulWidget {
MySearchWidget(
{Key? key,
required this.onChanged, this.isListClear})
: super(key: key);
final Function(String?) onChanged;
final Function(bool)? isListClear;
@override
State<MySearchWidget> createState() => _MySearchWidget();
}
class _MySearchWidget extends State<MySearchWidget> {
late FocusNode myFocusNode;
final TextEditingController _textController = TextEditingController();
@override
void initState() {
myFocusNode = FocusNode();
super.initState();
}
@override
void dispose() {
myFocusNode.dispose();
super.dispose();
}
_onBackPressed(context) {}
@override
Widget build(BuildContext context) {
return Row(
children: [
IconButton(
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
onPressed: () {
_onBackPressed(context);
},
icon: Icon(Icons.arrow_left),
),
Expanded(
child: Container(
alignment: Alignment.center,
padding: const EdgeInsets.only(right: 14),
child: TextFormField(
controller: _textController,
focusNode: myFocusNode,
autofocus: true,
onChanged: (value) => widget.onChanged(value),
decoration: InputDecoration(
suffixIcon: _textController.text.isNotEmpty
? GestureDetector(
onTap: () {
_textController.clear();
widget.isListClear!(true);
},
child: Container(
width: 10,
height: 10,
alignment: Alignment.center,
child: Icon(Icons.clear),
),
)
: const SizedBox(),
),
),
),
),
],
);
}
}
如果不这样使用,这个小部件的change方法将在初始化小部件时被调用,它将过滤您的数据中不需要的空字符串,这也会引发问题。
但实际的错误是空操作符。我猜你的publiccharingstationmodel类应该有'address'属性不为空。
所以你不应该使用
resultAddressesInStationList = stationsList
.where((element) => element.address!
.toLowerCase()
.contains(enteredKeyword.toLowerCase()))
.toList();
应该像
一样使用resultAddressesInStationList = stationsList
.where((element) => element.address
.toLowerCase()
.contains(enteredKeyword.toLowerCase()))
.toList();
或者将模型地址属性更改为nullable,并保持现在的过滤器。
class PublicChargingStationModel{
final String? address;
PublicChargingStationModel({required this.address});
}