I have tried multiple ways to arrange products according to their price from low to high but the program keeps showing error
class string/int has no method sort
I want to arrange the products list according to price and weight.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'ProductModel.dart';
class ProductScreen extends StatefulWidget {
final ValueSetter<ProductModel> _valueSetter;
final ValueSetter<ProductModel>_valueRemoval;
static bool name=false;
ProductScreen(this._valueSetter,this._valueRemoval);
@override
_ProductScreenState createState() => _ProductScreenState();
}
class _ProductScreenState extends State<ProductScreen> {
//Seller s=new Seller(myController, myController1, myController2, imageFile);
final List<ProductModel> products = [
ProductModel("A cow1", 300,"24",Image.network("https://images.unsplash.com/photo-1570042225831-d98fa7577f1e?ixlib=rb-1.2.1&w=1000&q=80")),
ProductModel("C cow2", 400,"43",Image.network("https://images.unsplash.com/photo-1570042225831-d98fa7577f1e?ixlib=rb-1.2.1&w=1000&q=80")),
ProductModel("B cow3",600,"67",Image.network("https://images.unsplash.com/photo-1570042225831-d98fa7577f1e?ixlib=rb-1.2.1&w=1000&q=80")),
ProductModel("E cow4", 1000,"123",Image.network("https://images.unsplash.com/photo-1570042225831-d98fa7577f1e?ixlib=rb-1.2.1&w=1000&q=80")),
ProductModel("D cow5", 900,"150",Image.network("https://images.unsplash.com/photo-1570042225831-d98fa7577f1e?ixlib=rb-1.2.1&w=1000&q=80")),
ProductModel("F cow6",1200,"189",Image.network("https://images.unsplash.com/photo-1570042225831-d98fa7577f1e?ixlib=rb-1.2.1&w=1000&q=80")),
//ProductModel(Seller.myController.text,int.parse(Seller.myController2.text),Seller.myController1.text,Image.file(Seller.imageFile),),
//myController etc are static memebers in sellers because only static members can be use in initializers of productModel
//Use classname.parameter because you can't acces static members through object of class like s.myController.text would be wrong
];
List<ProductModel> productsArranged=[];
filterByPrice(products) {
setState(() {
ProductScreen.name=true;
/*()productsArranged= products
.where((products) =>
(products.name).toLowerCase().contains(products.toLowerCase()))
.toList(); */
});
}
SortPrice(products){
products.sort((a,b)=>a.compareTo(b));
}
@override
Widget build(BuildContext context) {
return Container(
child:ListView.separated(
itemBuilder: (context, index){
return ListTile(
title:Text(products[index].name),
leading:products[index].i,
subtitle:Column(
children:<Widget>[
!ProductScreen.name?Text("\$${products[index].price}",
style: TextStyle(color: Colors.redAccent, fontSize: 20, fontWeight: FontWeight.w500),):SortPrice(products[index].price),
//Text(,style: TextStyle(color: Colors.redAccent, fontSize: 20, fontWeight: FontWeight.w500),),
//Text("\$${products[index].price}",style: TextStyle(color: Colors.redAccent, fontSize: 20, fontWeight: FontWeight.w500),),
Text(products[index].weight.toString()+" kg",
style: TextStyle(color: Colors.redAccent, fontSize: 20, fontWeight: FontWeight.w500),),
// SortPrice(products[index].weight),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
IconButton(
icon: Icon(Icons.remove_circle, color: Colors.green,),
onPressed: () {
widget._valueRemoval(products[index]);
SnackBar s = SnackBar(content: Text(
products[index].name + " removed from your cart"),
duration: Duration(milliseconds: 3),
backgroundColor: Colors.pinkAccent,);
Scaffold.of(context).showSnackBar(s);
},),
IconButton(icon: Icon(Icons.add_circle, color: Colors.red,),
onPressed: () {
widget._valueSetter(products[index]);
SnackBar s = SnackBar(content: Text(
products[index].name + " added to your cart"),
duration: Duration(milliseconds: 3),
backgroundColor: Colors.cyanAccent,);
Scaffold.of(context).showSnackBar(s);
},),
],),
], ),
onTap: (){
},
// ),
);
},
separatorBuilder: (context, index){
return Divider();
},
itemCount: products.length
),);
}
}
The below code is my ProductModel
import 'package:flutter/cupertino.dart';
class ProductModel{
String name;
int price;
String weight;
Image i;
ProductModel(this.name,this.price,this.weight,this.i)
}