0

I'm trying to sort my list alphabetically by item before showing it in a ListView.builder, and I'm not having any luck doing so. Here's my code:

class GlossaryItem {
  String item;
  String definition;

  GlossaryItem({String i, String d}) {
    item = i;
    definition = d;
  }
}

class Glossary {
  int _glossaryNumber = 0;

  List<GlossaryItem> _glossaryBank = [
    GlossaryItem(
      i: 'item a',
      d: 'definition a',
    ),
    GlossaryItem(
      i: 'item d',
      d: 'definition d',
    ),
    GlossaryItem(
      i: 'item b',
      d: 'definition b',
    ),
    GlossaryItem(
      i: 'item c',
      d: 'definition c',
    ),
  ];

  _glossaryBank.sort((a, b) {
  int compare = a.item.compareTo(b.item);
  return compare;
  });

  int getCount() {
    return _glossaryBank.length;
  }

  String getSpecificItem(index) {
    return _glossaryBank[index].item;
  }

  String getSpecificDefinition(index) {
    return _glossaryBank[index].definition;
  }
}

I get an error on _glossaryBank.sort() saying "The name of the constructor must match the name of the enclosing class." I have gone through many pages and can't seem to get this to work.

EDIT: I changed it the following and now I don't get an error on that page:

  void sort() {
  _glossaryBank.sort((a, b) {
    int compare = a.item.compareTo(b.item);
    return compare;
  });

However, when trying to initiate it, I get other errors. Here is my other page where I initiate it:

import 'package:flutter/material.dart';
import '../dictionary/glossarylist.dart';

//Call glossary
var glossary = Glossary();
glossary.sort();

class GlossaryPage extends StatefulWidget {
  GlossaryPage({
    Key key,
  });

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

class _GlossaryPageState extends State<GlossaryPage> {

  Widget printDefinitions() {
    return ListView.builder(
      itemCount: glossary.getCount(),
      itemBuilder: (BuildContext context, int index) {
        // return row
        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Text(
                glossary.getSpecificItem(index) + ':',
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                ),
              ),
              Text(
                glossary.getSpecificDefinition(index),
                style: TextStyle(
                  fontStyle: FontStyle.italic,
                ),
              ),
            ],
          ),
        );
      },
      shrinkWrap: true,
      physics: ClampingScrollPhysics(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.teal[300],
      child: SafeArea(
        child: Scaffold(
          appBar: [omitted code...]
          body: Padding(
            padding: const EdgeInsets.all(15.0),
            child: printDefinitions(),
          ),
        ),
      ),
    );
  }
}
1
  • You added a call to the sort method in the body of the class, without enclosing method. This is not allowed, and the compiler figured out that you must be trying to define a named constructor (dart.dev/guides/language/language-tour#named-constructors), hence gave you unhelpful error message: The name of the constructor must match the name of the enclosing class. Commented May 12, 2020 at 19:40

1 Answer 1

1

The problem is that you have the _glossaryBank outside of a running method. You have to write something where you can execute the code.

class Glossary {
  int _glossaryNumber = 0;

  List<GlossaryItem> _glossaryBank = [
    GlossaryItem(
      i: 'item a',
      d: 'definition a',
    ),
    GlossaryItem(
      i: 'item d',
      d: 'definition d',
    ),
    GlossaryItem(
      i: 'item b',
      d: 'definition b',
    ),
    GlossaryItem(
      i: 'item c',
      d: 'definition c',
    ),
  ];

  void sort() {
    _glossaryBank.sort((a, b) {
      int compare = a.item.compareTo(b.item);
      return compare;
    });
  }
}

and wherever you initiate the class, you have to use it

var glossary = Glossary();
glossary.sort();

I created a small CodePen about how it could work. https://codepen.io/md-weber/pen/bGVjdap

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

4 Comments

Thank you, but I'm getting the same error now on glossary.sort(); where I initiate the class.
If I call it at the top of the page outside of the class where my listview.builder is, it says Functions must have an explicit list of parameters. Expected a method, getter, setter or operator declaration. The name glossary is already defined. A function body must be provided. If I initiate it inside the same class as my listview.builder, it tells me The name of the constructor must match the name of the enclosing class.
I created a small CodePen maybe it helps to make it more clear: codepen.io/md-weber/pen/bGVjdap
Okay, I got it now. It's working perfectly. I just had to initiate it in the build section. Makes sense. Thanks a bunch!

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.