GithubHelp home page GithubHelp logo

Comments (8)

AhmedLSayed9 avatar AhmedLSayed9 commented on July 20, 2024

You can override the == operator for the object itself.
or, use some unique value at the object like an id to be used as value.

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on July 20, 2024

@vasilich6107
Can you check #127 and see if it's what you're looking for?

from dropdown_button2.

vasilich6107 avatar vasilich6107 commented on July 20, 2024

What if I have no access to object class?

Imagine that I'm getting user info with role object UserRole
another api returns a list of available roles List<UserRole>

I have to map this classes into custom objects to have an ability to override == operator
Otherwise I will have to use id as value and list of id's as items and build selected item with searching through the list of items. Ability to override the comparator will make it easy in case of refactoring if you have something already created but you need to change only comparison method

from dropdown_button2.

vasilich6107 avatar vasilich6107 commented on July 20, 2024

even if I have an ability to override == there are cases when you have a need to change the comparator method on the fly.

You have a list of items, that could be disabled on BE.
One entity assigned to disable person.
So be will return a list of only enabled persons.

at this point you have a need to inject this preselected item into the list of items with updated name 'deactivated` this is the case when it is nice to have comparator update.

Do you see any disadvantages on adding an ability to set custom comparator?

from dropdown_button2.

vasilich6107 avatar vasilich6107 commented on July 20, 2024

If it works for you I can create a PR

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on July 20, 2024

Do you see any disadvantages on adding an ability to set custom comparator?

You can set custom comparator by using value.

I'm not sure what value will this add over doing:

class _MyHomePageState extends State<MyHomePage> {
  final List<Item> items = const [
    Item('1', 'Item1'),
    Item('2', 'Item2'),
    Item('3', 'Item3'),
    Item('4', 'Item4'),
  ];
  final valueListenable = ValueNotifier<String?>(null);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: DropdownButtonHideUnderline(
          child: DropdownButton2<String>(
            isExpanded: true,
            hint: Text(
              'Select Item',
              style: TextStyle(
                fontSize: 14,
                color: Theme.of(context).hintColor,
              ),
            ),
            items: items
                .map((Item item) => DropdownItem<String>(
                      value: item.id,
                      height: 40,
                      child: Text(
                        item.title,
                        style: const TextStyle(
                          fontSize: 14,
                        ),
                      ),
                    ))
                .toList(),
            valueListenable: valueListenable,
            onChanged: (String? value) {
              valueListenable.value = value;
            },
            buttonStyleData: const ButtonStyleData(
              padding: EdgeInsets.symmetric(horizontal: 16),
              height: 40,
              width: 140,
            ),
          ),
        ),
      ),
    );
  }
}

class Item {
  const Item(this.id, this.title);

  final String id;
  final String title;
}

from dropdown_button2.

Mir1001 avatar Mir1001 commented on July 20, 2024

I am experiencing a similar issue. Specifically, I am retrieving a list of users from an API, which includes the currently logged-in user but with less detailed information (same ID, public data, etc.). When I try to set the selected item/object to the logged-in user, the object isn't found because the equality operator (==) is auto-generated by Freezed and I prefer not to modify it.

As a workaround, I could replace the logged-in user in the list if it exists or use just IDs as values, though this approach is less intuitive. What is best approach?

from dropdown_button2.

AhmedLSayed9 avatar AhmedLSayed9 commented on July 20, 2024

I am experiencing a similar issue. Specifically, I am retrieving a list of users from an API, which includes the currently logged-in user but with less detailed information (same ID, public data, etc.). When I try to set the selected item/object to the logged-in user, the object isn't found because the equality operator (==) is auto-generated by Freezed and I prefer not to modify it.

As a workaround, I could replace the logged-in user in the list if it exists or use just IDs as values, though this approach is less intuitive. What is best approach?

What's wrong with using id as value here?

from dropdown_button2.

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.