'type '(List<Animal>) => void' is not a subtype of type '((List<Animal?>) => void)?'
type 'List<Object?>' is not a subtype of type 'List<Animal?>' of 'function result'
giving me errors on saving selected values value is shown in debug but cant print value properly and only print "instance of class".
MultiSelectDialogField(
items: _items,
title: const Text("Animals"),
selectedColor: Colors.blue,
decoration: BoxDecoration(
color: Colors.blue.withOpacity(0.1),
borderRadius: const BorderRadius.all(Radius.circular(40)),
border: Border.all(
color: Colors.blue,
width: 2,
),
),
buttonIcon: const Icon(
Icons.pets,
color: Colors.blue,
),
buttonText: Text(
"Favorite Animals",
style: TextStyle(
color: Colors.blue[800],
fontSize: 16,
),
),
onConfirm: (results) {
//_selectedAnimals = results;
},
),
const SizedBox(height: 50),
//################################################################################################
// This MultiSelectBottomSheetField has no decoration, but is instead wrapped in a Container that has
// decoration applied. This allows the ChipDisplay to render inside the same Container.
//################################################################################################
Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColor.withOpacity(.4),
border: Border.all(
color: Theme.of(context).primaryColor,
width: 2,
),
),
child: Column(
children: <Widget>[
MultiSelectBottomSheetField(
initialChildSize: 0.4,
listType: MultiSelectListType.CHIP,
searchable: true,
buttonText: const Text("Favorite Animals"),
title: const Text("Animals"),
items: _items,
onConfirm: (values) {
_selectedAnimals2 = values;
},
chipDisplay: MultiSelectChipDisplay(
onTap: (value) {
setState(() {
_selectedAnimals2.remove(value);
});
},
),
),
_selectedAnimals2 == null || _selectedAnimals2.isEmpty
? Container(
padding: const EdgeInsets.all(10),
alignment: Alignment.centerLeft,
// ignore: prefer_const_constructors
child: Text(
"None selected",
style: const TextStyle(color: Colors.black54),
))
: Container(),
],
),
),
const SizedBox(height: 40),
//################################################################################################
// MultiSelectBottomSheetField with validators
//################################################################################################
MultiSelectBottomSheetField<Animal>(
key: _multiSelectKey,
initialChildSize: 0.7,
maxChildSize: 0.95,
title: const Text("Animals"),
buttonText: const Text("Favorite Animals"),
items: _items,
searchable: true,
validator: (values) {
if (values == null || values.isEmpty) {
return "Required";
}
List<String> names = values.map((e) => e.name).toList();
if (names.contains("Frog")) {
return "Frogs are weird!";
}
return null;
},
onConfirm: (values) {
setState(() {
_selectedAnimals3 = values;
});
_multiSelectKey.currentState!.validate();
},
chipDisplay: MultiSelectChipDisplay(
onTap: (item) {
setState(() {
_selectedAnimals3.remove(item);
});
_multiSelectKey.currentState!.validate();
},
),
),
const SizedBox(height: 40),
//################################################################################################
// MultiSelectChipField
//################################################################################################
MultiSelectChipField(
items: _items,
initialValue: [_animals[4], _animals[7], _animals[9]],
title: const Text("Animals"),
headerColor: Colors.blue.withOpacity(0.5),
decoration: BoxDecoration(
border: Border.all(color: Colors.blue.shade100, width: 1.8),
),
selectedChipColor: Colors.blue.withOpacity(0.5),
selectedTextStyle: TextStyle(color: Colors.blue[800]),
onTap: (values) {
_selectedAnimals4 = values;
},
),
const SizedBox(height: 40),
//################################################################################################
// MultiSelectDialogField with initial values
//################################################################################################
MultiSelectDialogField(
onConfirm: (values) {
_selectedAnimals5 = values;
},
dialogWidth: MediaQuery.of(context).size.width * 0.7,
items: _items,
initialValue:
_selectedAnimals5, // setting the value of this in initState() to pre-select values.
),
Heading
how to solve this error with this flutter dart dependency you have any solution????
Solution 1:[1]
It's just a null-safety issue. You should search for a good tutorial on that to better understand the meaning of the ? added at the end of a type.
From what I can see from the screenshot, you're passing a List<Animal> object where the code expects a List<Animal?> object: the difference is that the items in the first type CANNOT be null, while in the second type they MIGHT be null.
I'm guessing the problem is in the validatorof MultiSelectBottomSheetField<Animal> object. It's nice and fancy to just write things like (item) => something() but it's very easy to lose control on types.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | il_boga |

