Code Monkey home page Code Monkey logo

Comments (8)

lcuis avatar lcuis commented on July 29, 2024

Hi @joshwhitehouse ,

Thanks for reporting this issue.

It might be related to the way the search matching function is implemented.

Can you please share your code so that I can have a look?

from search_choices.

joshwhitehouse avatar joshwhitehouse commented on July 29, 2024

Hi,

Thanks for you reply! I'm using pretty much the same search matching function you are.

Here is my implementation code. The Countries object is from a database. Let me know if there is anything else you need.

BTW, this is a terrific package you've developed, I have tried a couple of others, but yours is definitely better for my use!

Best,

Josh

class LocationsCountrySearchWidget extends StatelessWidget {
  const LocationsCountrySearchWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return SearchChoices<Countries>.single(
      style: TextStyle(fontSize: 14, color: blueColor),
      hint: "Select a Country",
      searchHint: "Select a Country",
      onChanged: (country) {
// set a value in the Provider store
      },
      items: getCountries(context: context),
      value: null,
      searchFn: (String keyword, items) {
        List<int> ret = [];
        if (items != null && keyword.isNotEmpty) {
          keyword.split(" ").forEach((k) {
            int i = 0;
            items.forEach((item) {
              if (!ret.contains(i) &&
                  k.isNotEmpty &&
                  (item.child
                      .toString()
                      .toLowerCase()
                      .contains(k.toLowerCase()))) {
                ret.add(i);
              }
              i++;
            });
          });
        }
        if (keyword.isEmpty) {
          ret = Iterable<int>.generate(items.length).toList();
        }
        return (ret);
      },
      dialogBox: true,
      isExpanded: true,
      clearSearchIcon: const Icon(
        Icons.backspace,
        color: Color.fromRGBO(33, 150, 243, 1),
      ),
    );
  }

  List<DropdownMenuItem<Countries>>? getCountries(
      {required BuildContext context}) {
    final realmServices = Provider.of<RealmServices>(context, listen: false);
    late RealmResults<Countries> countries = realmServices.realm
        .query<Countries>('TRUEPREDICATE SORT(country_name ASC)');

    List<DropdownMenuItem<Countries>> dropDownList = countries
        .map((country) => DropdownMenuItem(
              value: country,
              child: Text(country.countryName),
            ))
        .toList();
    return dropDownList;
  }
}

from search_choices.

lcuis avatar lcuis commented on July 29, 2024

Hi @joshwhitehouse ,

Thanks a lot for your message, this means a lot to me!

I was not able to try your code but I spotted a difference with the example that may explain the behavior you noticed:

In your code:

item.child // Using the item.child means you are taking the Text Widget instead of a String value. When using .toString() on it while in debug, this may work but not in release.
                      .toString()
                      .toLowerCase()
                      .contains(k.toLowerCase())

In the example:

item.value // The example uses the item.value which, in the example, is a String.
                        .toString()
                        .toLowerCase()
                        .contains(k.toLowerCase())

Since your items contains a value which contains a property called countryName, what you want to do is probably the following:

Likely working in your case:

item.value.countryName // In your case, the value.countryName is likely the String you want to search against.
                        .toString()
                        .toLowerCase()
                        .contains(k.toLowerCase())

I hope this will solve your issue.

from search_choices.

joshwhitehouse avatar joshwhitehouse commented on July 29, 2024

I just noticed the same thing myself! I had already changed it to item.value.countryName instead of the item.child (which is a Text). It worked on the simulator just now using item.value.countryName, I'm going to test it on the iPhone right now, and will confirm it works as soon as I do. Many, many thanks!

from search_choices.

lcuis avatar lcuis commented on July 29, 2024

That's great, thanks for letting me know. Fingers crossed for the release/device test.

from search_choices.

joshwhitehouse avatar joshwhitehouse commented on July 29, 2024

Yes! that fixed it fine on the device as well! Thanks so much for having a look!

Best,

Josh

from search_choices.

joshwhitehouse avatar joshwhitehouse commented on July 29, 2024

closed

from search_choices.

lcuis avatar lcuis commented on July 29, 2024

Thanks to you Josh for this great news!

from search_choices.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.