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

Set initial selected values but getting exception "Exception: Cannot select options that are not in the options list" in version 2.1.4 #97

Open
nadeesh19 opened this issue May 15, 2024 · 2 comments

Comments

@nadeesh19
Copy link

nadeesh19 commented May 15, 2024

This is what i tried on flutter application.
i used this code with version 2.1.0 then no issue, but 2.1.4 is getting below exception.
please resolve it

import 'package:flutter/material.dart';
import 'package:multi_dropdown/multiselect_dropdown.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      themeMode: ThemeMode.dark,
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // final MultiSelectController<User> _controller = MultiSelectController();

  List<ValueItem> valueItemList = [];
  List<ValueItem> _selectedValueItemList = [ValueItem(label: 'Option 2', value: '2')]; // (1)

  @override
  void initState() {

    // _selectedGlobalLocationList = [ValueItem(label: 'Option 2', value: '2')]; //  (2)

    valueItemList = [
      ValueItem(label: 'Option 1', value: '1'),
      ValueItem(label: 'Option 2', value: '2'),
      ValueItem(label: 'Option 3', value: '3'),
      ValueItem(label: 'Option 4', value: '4'),
      ValueItem(label: 'Option 5', value: '5'),
      ValueItem(label: 'Option 6', value: '6'),];


    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey.shade300,
        appBar: AppBar(),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const SizedBox(
                    height: 50,
                  ),
                  MultiSelectDropDown(
                    onOptionSelected: (options) {
                      debugPrint(options.toString());
                      _selectedValueItemList = options;
                    },
                    selectedOptions: _selectedValueItemList,
                    options: (valueItemList).map((e) {
                      final item = e; //Map<String, dynamic>;
                      return ValueItem(
                        label: item.label != null ? item.label! : "",
                        value: item.value!,
                      );
                    }).toList(),
                    selectionType: SelectionType.multi,
                    chipConfig: const ChipConfig(wrapType: WrapType.wrap),
                    dropdownHeight: 400,
                    optionTextStyle: const TextStyle(fontSize: 16),
                    selectedOptionIcon: const Icon(Icons.check_circle),
                  ),

                  const SizedBox(height: 400)
                ],
              ),
            ),
          ),
        ));
  }
}

and getting this error but in UI it displays correctly. but individual de-select is not working. what is the issue here ?

======== Exception caught by scheduler library =====================================================
The following _Exception was thrown during a scheduler callback:
Exception: Cannot select options that are not in the options list

When the exception was thrown, this was the stack: 
#0      MultiSelectController.setSelectedOptions (package:multi_dropdown/multiselect_dropdown.dart:1176:7)
#1      _MultiSelectDropDownState._addOptions.<anonymous closure> (package:multi_dropdown/multiselect_dropdown.dart:422:21)
#2      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1386:15)
#3      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1322:11)
#4      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1169:5)
#5      _invoke (dart:ui/hooks.dart:312:13)
#6      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:399:5)
#7      _drawFrame (dart:ui/hooks.dart:283:31)
====================================================================================================
@nadeesh19 nadeesh19 changed the title Set initial selected values for but getting exception "Exception: Cannot select options that are not in the options list" in version 2.1.4 Set initial selected values but getting exception "Exception: Cannot select options that are not in the options list" in version 2.1.4 May 15, 2024
@mrizkynurfajrie
Copy link

To set initial values you should use their controller. Set the options & selected options from controller. You can use :

_controller.setOptions(...) // To set options, you should do this. The selected items will match an items from this options
_controller.setSelectedOptions(...) //  Then, set initial selected items from this controller function 

put that code into initState

@Nurullah1995
Copy link

I tried but same error
### The following _Exception was thrown during a scheduler callback:
Exception: Cannot select options that are not in the options list

final MultiSelectController  _controller = MultiSelectController();


List<ValueItem> valueItemList = [
  ValueItem(label: 'Option 1', value: '1'),
  ValueItem(label: 'Option 2', value: '2'),
  ValueItem(label: 'Option 3', value: '3'),
  ValueItem(label: 'Option 4', value: '4'),
  ValueItem(label: 'Option 5', value: '5'),
  ValueItem(label: 'Option 6', value: '6'),];

List<ValueItem> _selectedValueItemList = [ValueItem(label: 'Option 2', value: '2')];

@override
void initState() {
  // TODO: implement initState

  _controller.setOptions(valueItemList);
  _controller.setSelectedOptions(_selectedValueItemList) ;
  super.initState();
}




    MultiSelectDropDown(
          onOptionSelected: (options) {
            debugPrint(options.toString());
            _selectedValueItemList = options;
          },
          selectedOptions: _selectedValueItemList,
          options: (valueItemList).map((e) {
            final item = e; //Map<String, dynamic>;
            return ValueItem(
              label: item.label != null ? item.label! : "",
              value: item.value!,
            );
          }).toList(),
          selectionType: SelectionType.multi,
          chipConfig: const ChipConfig(wrapType: WrapType.wrap),
          dropdownHeight: 400,
          optionTextStyle: const TextStyle(fontSize: 16),
          selectedOptionIcon: const Icon(Icons.check_circle),
        ),

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

3 participants