Home>
I am using Flutter, Dart to make a dictionary application specialized for verbs.

I want to use contains to display a list that matches the characters that the user put in the search field, but the verb List is in Firestore, and when I try to link, the following error message occurs. did.

The method "contains" isn't defined for the class "VerbsData".
Try correcting the name of an existing method, or defining a method named "contains".
Applicable source code
class Search extends StatefulWidget {
  Search({Key key}): super(key: key);
  @override
  _Search createState() =>new _Search();
}
class _Search extends State<Search>{
  TextEditingController editingController = TextEditingController();
  final duplicateItems = List<VerbsData>();
  final items = List<VerbsData>();
  @override
  void initState() {
    items.addAll(duplicateItems);
    super.initState();
  }
  void filterSearchResults(String query) {
    List<VerbsData>dummySearchList = List<VerbsData>();
    dummySearchList.addAll(duplicateItems);
    if (query.isNotEmpty) {
      List<VerbsData>dummyListData = List<VerbsData>();
      dummySearchList.forEach((item) {
        if (item.contains(query)) {
          dummyListData.add(item);
        }

      });
      setState(() {
        items.clear();
        items.addAll(dummyListData);
      });
      return;
    } else {
      setState(() {
        items.clear();
        items.addAll(duplicateItems);
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return StreamProvider<List<VerbsData>>.value(
      value: DatabaseService().verbs,
      child: Scaffold(
        appBar: AppBar(
          leading: ButtonOnAppBar(),
          title: TextField(
            onChanged: (value) {
              filterSearchResults(value);
            },
            controller: editingController,
            decoration: InputDecoration(
                labelText: "Search",
                hintText: "Search",
                prefixIcon: Icon(Icons.search),
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(25.0)))),
          ),
        ),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children:<Widget>[DropdownForSorts(), WordsList()],
          ),
        ),
      ),
    );
  }
}
What I tried

I tried to define it in bool in the official document (https://api.dart.dev/stable/1.10.1/dart-core/List/contains.html), but the error did not change. Since I am a beginner in programming itself, I may not know the fundamental part...

Supplementary information

The source code (https://blog.usejournal.com/flutter-search-in-listview-1ffa40956685) that had fallen on the net was arranged in my own way, so I am using it without sufficient knowledge. .. If i have any inconsistencies in my remarks or source code, please let me know. Thank you.
I'm using macOS.

  • Answer # 1

    VerbsDataTocontainsThe error is that the method is not defined.
    VerbsDataI do not know what kind of content is from the question content, but in the Dart standard,StringOrListEtccontainsCan be used.

    StringExample

    var string ='Dart strings';
    string.contains('D');// true
    string.contains('d');// false

    ListExample

    var numbers = [1, 3, 2, 5, 4];
    print(numbers.contains(1));// true
    print(numbers.contains(6));// false

    In the question "source code that was on the net",StringAgainstcontainsI am using.
    The points to be corrected are as follows.

    if (item.contains(query)) {


    itemIsVerbsDataBecause it isVerbsDataAt the innercontainsAn error will occur unless you define. OrVerbsDataIs withinStringSee type variable.