'Can Anyone solve Custom DropDownButton issue. Flutter ( i know many will not be able to )

TO SOLVE THIS U NEED TO RUN IT FIRST

Only for them who have experience with DropDownButton and T typepassing can solve this. Please Help!



import 'package:flutter/material.dart';

// ignore: must_be_immutable
class SupDropDownButton<T> extends StatefulWidget {
  FormFieldValidator<T>? validator;
  ValueChanged<T> value;
  final List<T> data;
  SupDropDownButton(
      {Key? key, required this.data, this.validator, required this.value})
      : super(key: key);

  @override
  State<SupDropDownButton> createState() => _SupDropDownButtonState<T>();
}

class _SupDropDownButtonState<T> extends State<SupDropDownButton> {
  T? _value;

  List<DropdownMenuItem<T>> items() =>
      widget.data.cast<T>().map<DropdownMenuItem<T>>(menuItem).toList();

  DropdownMenuItem<T> menuItem(dynamic value) => DropdownMenuItem<T>(
        value: value,
        child: Text(value.name),
      );

  @override
  Widget build(BuildContext context) {
    return DropdownButtonFormField<T>(
      decoration: const InputDecoration(border: InputBorder.none),
      validator: widget.validator,
      value: _value,
      onChanged: (T? val) {
        FocusScope.of(context).requestFocus(FocusNode());
        _value = val!;
        widget.value.call(val);
        setState(() {});
      },
      items: items(),
      hint: const Text('Please select Categories'),
    );
  }
}

THIS IS THE ERROR

Expected a value of type ((dynamic) => String?)?, but got one of type (Employee) => String?


Solution 1:[1]

I have worked on your code. Instead of value.name in your code, I have directly add List<String> for easy reference and it's working fine.I using null safety, that's why add late initialize to data list.if you need to create object and insert JSON data means revert back will rework on it

static data added like List<String> employeeList = ['hari','chetanPatil'] and cast it to data;

Working code :

// ignore: must_be_immutable
class SupDropDownButton<T> extends StatefulWidget {
 FormFieldValidator<T>? validator;
 ValueChanged<T> value;
 late List<T> data;
 SupDropDownButton(
     {Key? key, required this.data, this.validator, required this.value})
     : super(key: key);

 @override
 State<SupDropDownButton> createState() => _SupDropDownButtonState<T>();
}

class _SupDropDownButtonState<T> extends State<SupDropDownButton> {
 T? _value;
 List<String> employeeList = ['hari', 'chetanPatil'];
 bool isOnLoad = true;

 @override
 void initState() {
   super.initState();
 }

 List<DropdownMenuItem<T>> items() =>
     widget.data.cast<T>().map<DropdownMenuItem<T>>(menuItem).toList();

 DropdownMenuItem<T> menuItem(dynamic value) => DropdownMenuItem<T>(
       value: value,
       child: Text(value),
     );

 @override
 Widget build(BuildContext context) {
   widget.data.clear();
   widget.data.addAll(employeeList);
   return Scaffold(
     body: Center(
       child: Padding(
         padding: const EdgeInsets.all(40.0),
         child: DropdownButtonFormField<T>(
           decoration: const InputDecoration(border: InputBorder.none),
           validator: widget.validator,
           value: _value,
           onChanged: (T? val) {
             FocusScope.of(context).requestFocus(FocusNode());
             _value = val!;
             print(_value);
             widget.value.call(val);
             setState(() {});
           },
           items: items(),
           hint: const Text('Please select Categories'),
         ),
       ),
     ),
   );
 }
}

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 Yeasin Sheikh