简体   繁体   中英

How to add mapping of List in DropDownField() in flutter

i added a list that maps values type and s_id that worked on DropdownButtonFormField() but it is not working in DropDownField().

    class _serv_detailsState extends State<serv_details> {
     List dropList =[
       {"s_id":0, "type": "<Select>"},
       // {"edpt_id":1, "name": "<two>"},
      ];
      var fkey=GlobalKey<FormState>();
     var val=true;
      var dropdownValue = "";
     late TextEditingController details,type,fee;
      void gendrop() async {
      String url = login.url+"subscription/su/";
      var resp = await get(url);
      print(resp.body);
      setState(() {
      dropList = jsonDecode(resp.body);
      });
      }
     @override
     void initState() {
     details=TextEditingController();
     type=TextEditingController();
     fee=TextEditingController();
      // TODO: implement initState
     super.initState();
     gendrop();
     }

inside scaffold:

              Container(
              child: DropDownField(
                controller: type,
                hintText: "select the type",
                enabled: true,
                onValueChanged: (value){
                  setState(() {
                    dropdownValue = value.toString();
                        print(dropdownValue);

                          });

                },

                items: dropList.map((item) => DropdownMenuItem(child: Text(item['type'].toString()),value:item['s_id'].toString(),)).toList()


              )
            ),

The list is being shown in the output but when i clicked on the drop button it shows error

Expected a value of type 'List', but got one of type 'List<DropdownMenuItem>'

any help would be appreciated.

If you want to use DropDownField you have to use a List<String> as list for your items:

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      home: Scaffold(body: Center(
        child: DropDownField(
          items: [
            'helloWorld',
            'helloWorld',
            'helloWorld'
          ] as List<String>,
        ),
      )),
    );
  }
}

But maybe the DropDownButton ( https://api.flutter.dev/flutter/material/DropdownButton-class.html ) would be a nice solution for you, as you can use a List<Widget> for the item list.

Update

Try it like this:

 Container(
              child: DropDownField(
                controller: type,
                hintText: "select the type",
                enabled: true,
                onValueChanged: (value){
                  setState(() {
                    dropdownValue = value.toString();
                        print(dropdownValue);

                          });

                },

                items: dropList.entries.map((e) => e.key).toList(),

              )
            ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM