简体   繁体   中英

how to get selections from multi_select_flutter?

has to be something simple I'm not getting but have been struggling on this for way too long. If anybody can help me out I would be appreciative. I believe it has to do with some kind of type issues as when I make selections I have a print statement that then prints out what I'm getting back:

[Instance of 'MultiSelectItem', Instance of 'Analysis', Instance of 'Analysis']

Not sure how to convert the above to a List of Analysis and not able to carry out per the example for the package:

MultiSelectDialogField(
  items: _animals.map((e) => MultiSelectItem(e, e.name)).toList(),
  listType: MultiSelectListType.CHIP,
  onConfirm: (values) {
    _selectedAnimals = values;
  },
),

where I also have to use a 'cast' in my code. My attempt:

MultiSelectDialogField(
                        items: _analyses.map((analysis) => MultiSelectItem<Analysis>(analysis, analysis.name)).toList(),
                        initialValue: outwardsData.outwards[widget.index].lossFeed
                            .map((analysis) => MultiSelectItem<Analysis>(analysis, analysis.name))
                            .toList(),
                        title: Text(
                          "Loss Feeds:",
                          style: TextStyle(color: Colors.black),
                        ),
                        selectedColor: kConvexGreen,
                        unselectedColor: kConvexLightGreen,
                        backgroundColor: kConvexGreen,
                        decoration: BoxDecoration(
                          color: kConvexGreen.withOpacity(0.1),
                          borderRadius: BorderRadius.all(Radius.circular(5)),
                          border: Border.all(
                            color: kConvexGreen,
                            width: 2,
                          ),
                        ),
                        confirmText: Text(
                          'Submit',
                          style: TextStyle(color: Colors.white),
                        ),
                        cancelText: Text(
                          'Cancel',
                          style: TextStyle(color: Colors.white),
                        ),
                        searchable: true,
                        buttonText: Text("Loss Feeds"),
                        onSelectionChanged: (results) {
                          **print(results)**;
                        },
                        onConfirm: (results) {
                          // this is different than what example shows; without cast it errors
                          // "A value of type 'List<Object?>' can't be assigned to a variable of type 'List<Analysis>'". (Documentation)
                          _selectedAnalyses = results.cast();
                        },
                        chipDisplay: MultiSelectChipDisplay(
                          chipColor: kConvexGreen,
                          textStyle: TextStyle(color: Colors.white),
                          onTap: (value) {
                            setState(() {
                              _selectedAnalyses.remove(value);
                              // crashes here even though both are of same type List<Analyses> (theoretically)
                              // this is just here for testing purposes as am through many iterations trying to get 
                              // data into lossFeed.
                              outwardsData.outwards[widget.index].lossFeed =
                                  _selectedAnalyses; 
                            });
                          },
                        ),
                      ),

I'm sure a more experienced flutter person will understand this (maybe) but I found that I couldn't just have the initial values come from the same list type used for the item (List< Analysis> _analyses) but I actually had to map it to use the same items data (_analyses).

initialValue: outwardsData.outwards[widget.index].lossFeed.map((analysis) => _analyses[analysis.id - 1]).toList()

Had other problems with this widget such as I found I couldn't map the results from onConfirm directly to List but instead had to do the following:

_rData.lossFeed = results.toList().cast<Analysis>()

as the data came back as List< dynamic>.

Couldn't find great examples online and trust experience will be the cure-all at some point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM