Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'T' doesn't conform to the bound 'Object' of the type parameter 'T'. Try using a type that is or is a subclass of 'Object'. #153

Open
divan opened this issue Sep 13, 2024 · 2 comments

Comments

@divan
Copy link

divan commented Sep 13, 2024

Finally decided to upgrade to 3.x.x version. I'm using multidropdown in my own wrappers (for styling and convenience) and they all started throwing error:

'T' doesn't conform to the bound 'Object' of the type parameter 'T'. Try using a type that is or is a subclass of 'Object'.

The simplest possible way to reproduce it:

class MyMultiSelect<T> extends StatelessWidget {
  final List<T> items;

  MyMultiSelect(this.items, {super.key});

  final controller = MultiSelectController<T>();

  @override
  Widget build(BuildContext context) {
    return MultiDropdown<T>(
      items: toDropdownItems(items),
      controller: controller,
    );
  }

  List<DropdownItem<T>> toDropdownItems(List<T> items) {
    return items
        .map((e) => DropdownItem(
              label: e.toString(),
              value: e,
            ))
        .toList();
  }
}

It turns out, MultiDropdown widget specifies T as T extends Object. That's why T in my class becomes not compatible. It doesn't make sense to me, given that every class supposedly extends Object class anyway, but this is breaking widgets like mine. The solution is to add extends Object to the wrapper class:

class MyMultiSelect<T extends Object> extends StatelessWidget {

(and in the State class as well for stateful widgets).

Not sure why MultDropdown needs this 'extends Object', but this solution worked for me.

PS. Upgrading library and renaming everything and hoping that it will just work for library users is simply nuts. 3.x.x should've been a new package as it has little to do with 2.x.x.

@singgihmardianto
Copy link

I thinks it's because they also use a generic type that extends Object so basically you need to pass any type that extends Object.

@divan
Copy link
Author

divan commented Sep 14, 2024

@singgihmardianto yes, but why is it needed? Every type in dart already extending Object by definition.

The only reason I can think of is for not allowing null to be passed, but I'm struggling to undertand the real world scenario where this is something that real humans would write.

I have many custom wrappers around different widgets for my form fields, and this one is the only one that requires extends Object to be specified in order to compile (and it's not documented anywhere).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants