Reputation: 89
This is where I use the dropdown:
class _JobFunctionState extends State<JobFunction> {
static const jobList = <String>["Item 1", "Item 2", "Item 3"];
String dropdownValue = jobList[0];
@override
Widget build(BuildContext context) {
return Dropdown<String>(
hint: const Text("Choose job function"),
labelText: "Job Function",
value: dropdownValue,
dropdownMenuItemList: jobList
.map<DropdownMenuItem<String>>(
(String job) => DropdownMenuItem<String>(
value: job,
child: Text(job),
))
.toList(),
onChanged: (newDropdownValue) {
setState(() {
dropdownValue = newDropdownValue;
});
},
);
}
Here's the full dropdown class:
class Dropdown<T> extends StatefulWidget {
final List<DropdownMenuItem<T>> dropdownMenuItemList;
final ValueChanged<T> onChanged;
final T value;
final bool isBorder;
final double radius;
final TextStyle? textStyle;
final Color? color;
final Widget hint;
final String labelText;
const Dropdown(
{Key? key,
required this.dropdownMenuItemList,
required this.onChanged,
required this.value,
this.isBorder = true,
this.radius = 10.0,
this.textStyle,
this.color,
required this.hint,
required this.labelText})
: super(key: key);
_DropdownState<T> createState() => _DropdownState();
}
class _DropdownState<T> extends State<Dropdown> {
@override
Widget build(BuildContext context) {
return FormField<T>(
builder: (FormFieldState<T> state) {
return SingleChildScrollView(
child: DropdownButtonFormField<T>(
isExpanded: true,
itemHeight: 50.0,
items: widget.dropdownMenuItemList as List<DropdownMenuItem<T>>,
onChanged: widget.onChanged,
value: widget.value,
dropdownColor: Colors.white,
iconEnabledColor: Colors.grey,
icon: const Icon(Icons.arrow_drop_down),
hint: widget.hint,
),
);
},
);
}
}
and ValueChanged is:
typedef ValueChanged<T> = void Function(T value);
Dropdown is generic, I've made everything string in the jobFunction widget, and I get this error.
if I add dynamic to onChanged parameter(), but the type of the parameter should not be dynamic, but String. Any ideas?
adding text to fill the requirements for edit: slkdjf s;lkdjfsd jfiosdj fsdnf lksdjf klsjdfi skjldfj slkdj flksdjlkifj sf kjsdlk;fj slk;dj fisjd fiosj f;ajof hsiod jfsajfkl sjd fk jsdlf sdlkf lksjdfoijsfoi jsdlkjf lksadj flksdjflk sjdalkf jsakj fjsaoif jseij flisd jflksajflk jasdlk
Upvotes: 4
Views: 3694
Reputation: 2024
Remember also to change the overrided createState
method:
@override
_BasicDropdownState<T> createState() => _BasicDropdownState<T>();
Upvotes: 9
Reputation: 206
I got the same issue and what did work for me was replacing :
class _DropdownState<T> extends State<Dropdown>
by this:
class _DropdownState<T> extends State<Dropdown<T>>
Upvotes: 19
Reputation: 4404
Dropdown is generic, I've made everything string in the jobFunction widget, and I get this error.
ValueChanged should be nullable value. If you specific to a String
, then the onchange will specific to String?
: means String or null.
Because if there is no changes, it must be null.
how to assign the String?
to your variable that non-null
String is by add a condition like below.
onChanged: (newDropdownValue) {
if( newDropdownValue != null){
setState(() {
dropdownValue = newDropdownValue!;
});
}
},
if no changes happend, then you dont need to set new value to you dropdownValue
hope this solve your issue.
Upvotes: 1