hi all I'm trying to create a search bar but I can't I'm quite new I have a list in a CartModel class that I display in a FruitsPage class but I can't filter the search result.
CartModel
class CartModel extends ChangeNotifier {
final List _shopItems = const [
// [ itemName, itemPrice, imagePath, color ]
["Avocado", "4", "assets/images/8c.png", Colors.green],
["Banana", "2", "assets/images/8c.png", Colors.yellow],
["pollo", "12", "assets/images/8c.png", Colors.brown],
["acqua", "1", "assets/images/8c.png", Colors.blue],
["mela", "3", "assets/images/8c.png", Colors.red],
["broccoli", "3", "assets/images/8c.png", Colors.brown],
];
get shopItems => _shopItems;
GroceryItemTile
class GroceryItemTile extends StatelessWidget {
final String itemName;
final int itemPrice;
final String imagePath;
// ignore: prefer_typing_uninitialized_variables
final color;
void Function()? onPressed;
GroceryItemTile({
super.key,
required this.itemName,
required this.itemPrice,
required this.imagePath,
required this.color,
required this.onPressed,
});
FruitsPage
class FruitsPage extends StatefulWidget {
const FruitsPage({super.key});
@override
State<FruitsPage> createState() => _FruitsPageState();
}
class _FruitsPageState extends State<FruitsPage> {
String _searchText = '';
List _filteredShopItems = [];
@override
void initState() {
super.initState();
_filteredShopItems = cartmode.shopItems;
}
void _filterShopItems(String query) {
setState(() {
_searchText = query;
_filteredShopItems = cartmode.shopItems
.where((item) => item[0].toLowerCase().contains(query.toLowerCase()))
.toList();
});
}
CartModel cartmode = CartModel();
@override
@override
Widget build(BuildContext context) {
Device.screenType == ScreenType.tablet;
return Container(
margin: EdgeInsets.only(top: 20),
width: 100.w,
height: 100.5.h,
child: Container(
width: 100.w,
height: 20.5.h,
child: Consumer<CartModel>(builder: (context, value, child) {
return Scaffold(
appBar: AppBar(
title: CupertinoSearchTextField(
autocorrect: true,
onSubmitted: (value) {
_filterShopItems(value);
},
),
backgroundColor: Colors.white,
elevation: 0,
iconTheme: IconThemeData(
color: Colors.grey[800],
)),
body: GridView.builder(
padding: const EdgeInsets.all(12),
//physics: const NeverScrollableScrollPhysics(),
itemCount: value.shopItems.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1 / 1.2,
),
itemBuilder: (context, index) {
return GroceryItemTile(
itemName: value.shopItems[index][0].toString(),
itemPrice: int.parse(
value.shopItems[index][1].toString()),
imagePath: (value.shopItems[index][2]),
color: value.shopItems[index][3],
onPressed: () =>
Provider.of<CartModel>(context, listen: false)
.addItemToCart(index),
);
},
),
);
})));
}
}
I would be happy if someone can give me some advice, I don't understand why the search doesn't give me results, and it also prints an error in the console, namely 'The following _TypeError was thrown while calling onSubmitted for TextInputAction.search: type '(dynamic) => dynamic' is not a subtype of type '(dynamic) => bool' of 'test''
>. We want to lowercase `"Avocado"`, not `["Avocado", "4", "assets/images/8c.png", Colors.green]`
– Yukinosuke Takada Apr 21 '23 at 06:59