1

I'm making an app on flutter in which the items such as price in products List must be arranged in increasing order. After searching on the internet I came up with the solution of using compareTo and sort function but unfortunately, I'm having an error on sort() i.e: The expression here has a type void and therefore can't be used. The whole proper code is on Github https://github.com/MaidaFarooqi9/Application/blob/master/lib/ProductScreen.dart And the code part which I'm trying to modify is

return Container(
      child:ListView.separated(
        itemBuilder: (context, index){
          return ListTile(
           title:Text(products[index].name),
              leading:products[index].i,
              subtitle:Column(
                children:<Widget>[
                  if (!ProductScreen.name) Text("\$${products[index].price}",
                    style: TextStyle(color: Colors.redAccent, fontSize: 20, fontWeight: FontWeight.w500),)
                  else
               Text(products.sort((a,b)=>a.price.compareTo(b.price))) ,

The if-else condition is showing at if() the unsorted list and after else the price will be in ascending order

1 Answer 1

5

Just check out this example I have created from your given sample code :

import 'package:flutter/material.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(child: SampleApp()),
      ),
    );
  }
}

class SampleApp extends StatefulWidget {
  SampleApp({Key key}) : super(key: key);

  @override
  _SampleAppState createState() => _SampleAppState();
}

class _SampleAppState extends State<SampleApp> {
  var products = [
    Product("sample", 12),
    Product("name", 50),
    Product("azpc", 78),
    Product("hplm", 3),
    Product('ampl', 2),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    products.sort((a, b) => a.price.compareTo(b.price));
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.separated(
          itemBuilder: (context, index) {
            return Card(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(products[index].name),
                    Text(products[index].price.toString()),
                  ],
                ),
              ),
            );
          },
          separatorBuilder: (context, int) => Divider(),
          itemCount: products.length),
    );
  }
}

class Product {
  final String name;
  final double price;

  Product(this.name, this.price);
}

Let me know if it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Just make an upvote so that it gets complete and would be helpful for others as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.