一切正常,但是ListView不在onChanged上更新。虽然我正在使用setstate,但是它仍然没有过滤数据。注意(列表已在我打印时被过滤。唯一的问题是屏幕没有更新)
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:travel_together_passenger/Models/user.dart';
import 'package:travel_together_passenger/Screens/User_Available_Carpools_TILE.dart';
class User_AvailableCarpools_LIST extends StatefulWidget {
@override
_User_AvailableCarpools_LISTState createState() =>
_User_AvailableCarpools_LISTState();
}
class _User_AvailableCarpools_LISTState
extends State<User_AvailableCarpools_LIST> {
@override
Widget build(BuildContext context) {
final checkcarpools = Provider.of<List<CarpoolDataShow>>(context) ?? [];
final availablecarpools =
checkcarpools.where((element) => element.seats > 0).toList() ?? [];
List filteredcarpools =
checkcarpools.where((element) => element.seats > 0 ).toList() ?? [];
return Scaffold(
body: Column(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(hintText: 'Search..'),
onChanged: (text) {
setState(() {
filteredcarpools = availablecarpools
.where((element) => element.pickupaddress
.toLowerCase()
.contains(text.toLowerCase()))
.toList();
});
},
)),
Expanded(
flex: 10,
child: ListView.builder(
itemCount: filteredcarpools.length,
itemBuilder: (context, index) {
return User_AvailableCarpools_TILE(filteredcarpools[index]);
},
),
),
],
),
);
}
}
答案 0 :(得分:0)
以下代码正在运行。非常感谢pskink给了我提示。
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:travel_together_passenger/Models/user.dart';
import 'package:travel_together_passenger/Screens/User_Available_Carpools_TILE.dart';
class User_AvailableCarpools_LIST extends StatefulWidget {
@override
_User_AvailableCarpools_LISTState createState() => _User_AvailableCarpools_LISTState();
}
class _User_AvailableCarpools_LISTState extends State<User_AvailableCarpools_LIST> {
String txt = '';
@override
Widget build(BuildContext context) {
final checkcarpools = Provider.of<List<CarpoolDataShow>>(context) ?? [];
final availablecarpools =
checkcarpools.where((element) => element.seats > 0).toList() ?? [];
List filteredcarpools =
availablecarpools.where((element) => element.pickupaddress.toLowerCase().contains(txt.toLowerCase())).toList() ?? [];
return Scaffold(
body: Column(
children: [
Expanded(
child: TextField(
decoration: InputDecoration(
hintText: 'search..'
),
onChanged: (text)
{
setState(() {
txt=text;
});
print(filteredcarpools);
},
),
),
Expanded(
flex: 10,
child: ListView.builder(
//shrinkWrap: true,
key: UniqueKey(),
itemCount: filteredcarpools.length,
itemBuilder: (context, index) {
// print(filteredcarpools);
return User_AvailableCarpools_TILE(filteredcarpools[index]);
},
),
),
],
),
);
}
}