hiddencaliber / flutter_multiselect

Flutter package for multi-select UI widget
Other
52 stars 37 forks source link

Class 'BuildingModel' has no instance method '[]' #28

Closed pkmangukiya closed 3 years ago

pkmangukiya commented 4 years ago

When i pass dataSource as Model class List<BuildingModel> buildingList = []; then gives me this error

Class 'BuildingModel' has no instance method '[]'.
Receiver: Instance of 'BuildingModel'
Tried calling: []("number")

My code is

 Widget multiSelection(){
    return MultiSelect(
        buttonBarColor: Colors.white,
        autovalidate: true,
        hintText: "",
        titleText: 'Country of Residence',
//                    maxLength: 5, // optional
        validator: (dynamic value) {
          if (value == null) {
            return 'Please select one or more option(s)';
          }
          return null;
        },
        errorText: 'Please select one or more option(s)',
        dataSource: buildingList,
        textField: 'number',
        valueField: 'number',
        filterable: true,
        required: true,
        onSaved: (value) {
          print('The value is $value');
        },
        selectIcon: Icons.arrow_drop_down_circle,
        saveButtonColor: Colors.white,
        saveButtonTextColor: Colors.deepPurple,
        checkBoxColor: Theme.of(context).primaryColorDark,
        cancelButtonColor: Colors.white,
        cancelButtonTextColor: Colors.deepPurple,
        selectedOptionsBoxColor: Colors.white
    );
  }
hiddencaliber commented 4 years ago

@pkmangukiya : This plugin does not support dart model/ collection as a datasource. Please use JSON encode/ decode and pass JSON Array as datasource.

Thanks

pkmangukiya commented 4 years ago

So if i want to use this dart model/collection as a datasource then there is any other way? because of mostly dart model/collection is used instead of JSON.

hiddencaliber commented 4 years ago

@pkmangukiya As of now using model is not supported by this plugin. And yeah, if you want to get the values then you need to use this field inside a form. Please check the example available with this plugin. Thanks

debbsefe commented 3 years ago

I was able to get a dynamic list like this;

dataSource: allUsers.map((e) {
                            return {
                              "display": e.email,
                              "value": e.id,
                            };
                          }).toList(),
hiddencaliber commented 3 years ago

@debbsefe : Very good suggestion.

Everyone please use the debbsefe's suggestion.

Thanks mate!