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

FormBuilderDropdown assertion bug #220

Closed
kasangasj opened this issue Jan 18, 2020 · 8 comments
Closed

FormBuilderDropdown assertion bug #220

kasangasj opened this issue Jan 18, 2020 · 8 comments
Labels
duplicate This issue or pull request already exists question Further information is requested

Comments

@kasangasj
Copy link

I have two FormBuilderDropdowns which depend on each other, one is for categories and the other is for sub categories. When I change category then sub categories are loaded. This works fine as long as there is no selected subcategory but if I select a category, then select one of its sub categories it throws the following exception:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building FormField-[LabeledGlobalKey<FormFieldState>#3f353](dirty, dependencies: [_FormScope], state: FormFieldState#68c0b):
There should be exactly one item with [DropdownButton]'s value: {id: 3ebc51ba390511eab6a06b0f2e982c39, name: Self Referral, code: 0002, category_id: 3eb329ef390511eab6a029aa47757af9, category_name: User Fee, active: 1, deleted: 0}.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem item) {
return item.value == value;
}).length == 1'

Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=BUG.md

The relevant error-causing widget was:
FormBuilderDropdown-[<'visit_subcategory'>] file:///home/kasanga/AndroidStudioProjects/mobile/lib/dialogs/client_dialog.dart:119:19
When the exception was thrown, this was the stack:
#2 new DropdownButton (package:flutter/src/material/dropdown.dart:805:15)
#3 _FormBuilderDropdownState.build. (package:flutter_form_builder/src/fields/form_builder_dropdown.dart:118:26)
#4 FormFieldState.build (package:flutter/src/widgets/form.dart)
#5 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)
#6 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4223:15)
...
═════════════════════

How can I clear the subcategories drop down's value when the categories drop down value changes?

@maelfosso
Copy link

Hello,

I have the same of error too
Screenshot from 2020-02-19 14-19-25

List<DropdownMenuItem> items = this.kv.entries
        .map((entry) => DropdownMenuItem(
            value: entry.key,
            child: Text(entry.value)
      )).toList();
return FormBuilderDropdown(
      attribute: this.id,
      decoration: InputDecoration(labelText: this.title),
      // initialValue: value,
      // initialValue: ,
      hint: Text('Select a choice'),
      items: items
    );

I don't know what is the problem.

I set a FormBuilder into a PageView. When It appear at the first time, everything is OK. But if I don't fill it and move forward then I swipe back there, I got that error.

Here is the FormBuilder initialization

return FormBuilder(
      key: _fbKey,
      initialValue: data.map((key, value) {
        KindType p = this.ktypes.firstWhere((k) => k.id == key);
        switch (p.job) {
          case 'student':
            return MapEntry(key, value);
          case 'teacher':
            return MapEntry(key, value);
          case 'administrator':
            return MapEntry(key, DateTime.tryParse(value));
          case 'family':
            return MapEntry(key, value);
          default:
            return null; 
        }
      }),
      autovalidate: true,
      child: Column(
        children: this.ktypes.map((q) => q.build()).toList()
      )
    );

Any clues for me, please?

@danvick
Copy link
Collaborator

danvick commented May 7, 2020

Hi @kasangasj / @maelfosso,
Flutter expects that the value that's selected on the dropdown button is part of the items to choose from.

Note that if you update the list of DropdownMenuItems that condition may not continue to hold - thus the assertion error.

Therefore if you change the items mid-flight remember to set the value to null. Another alternative is to ignore (though not advisable) because assertion errors don't appear in production.

Sorry for the late response, I thought I had addressed this issue since it's actually a duplicate.

@danvick danvick added duplicate This issue or pull request already exists question Further information is requested labels May 7, 2020
@danvick danvick closed this as completed Jul 5, 2020
@2shrestha22
Copy link

Therefore if you change the items mid-flight remember to set the value to null. Another alternative is to ignore (though not advisable) because assertion errors don't appear in production.

Hi @danvick How to set the value to null, there is not value field like in default dropdownbutton?

@fuzes
Copy link

fuzes commented Mar 31, 2022

Therefore if you change the items mid-flight remember to set the value to null. Another alternative is to ignore (though not advisable) because assertion errors don't appear in production.

Hi @danvick How to set the value to null, there is not value field like in default dropdownbutton?

Hi @2shrestha22 Did you find a way to change it to null or default value?

@2shrestha22
Copy link

I stopped using the package.

@dev-thinks
Copy link

dev-thinks commented Apr 1, 2022

probably this might work, @fuzes

formkey.currentState.fields['yourdropdownfieldname'].didChange(null);

@miguelflores1993
Copy link

With the official flutter dropdown this does not happen, fix it or I will stop using the library

@deandreamatias
Copy link
Collaborator

Fix it or I will stop using the library

Hi @miguelflores1993 . This is a open source package about forms. If you can contribute to solve this problem, will be awesome. If not, we do not seen a problem if you use another package for implement your forms.
The open source does not feel threatened because you stop using something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists question Further information is requested
Projects
None yet
Development

No branches or pull requests

8 participants