Search code examples
flutterdartconstructorsetrequired

How to initialized Set variable in dart?


I use dart 2.13.3 . In my flutter app I can't initialized Set variable without required annotation.I want to build constructor without final Set<V>initialSelectedValues; or without required, because from another call this parameter is just optional. But show error and suggest me to add required annotation at the front of final Set<V>initialSelectedValues;. How to I change and solve this error?

class MultiSelectDialog<V> extends StatefulWidget {
  MultiSelectDialog({required this.items,this.initialSelectedValues});

  final List<MultiSelectDialogItem<V>> items;
  final Set<V>initialSelectedValues;

  @override
  State<StatefulWidget> createState() => _MultiSelectDialogState<V>();
}
final selectedValues = await showDialog<Set<int>>(
      context: context,
      builder: (BuildContext context) {
        return MultiSelectDialog(
          items: items,
          //initialSelectedValues: [1,2].toSet(),
        );
      },
    );

Solution

  • Now, I can solve this. Change to default parameter with const .

    MultiSelectDialog({required this.items,this.initialSelectedValues = const {}});
    
      final List<MultiSelectDialogItem<V>> items;
      final Set<V> initialSelectedValues;