i want to display data in DropDownButton From rest api using flutter

若如初见. 提交于 2021-02-10 19:51:08

问题


I was trying to get data from api and display it into DropDownButton I want to get country name and country id from api

My code is:

My get function


  List country_data = List();

  String countryid;

  Future<String> country() async {
    final prefs = await SharedPreferences.getInstance();

    final key = 'session_id';
    final value = prefs.get(key ) ?? 0;

    var res = await http.get(
        Uri.encodeFull("http://husam.from-ar.com/api/api-ad/en/1"),
        headers: {"Accept": "application/json",
                 "Authorization" : "$value",
        }); //if you have any auth key place here...properly..
    var resBody = await json.decode(res.body);

    setState(()  {
      country_data =  resBody;

    });

    return "Sucess";
  }












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

the display column:

Column(
                 children: <Widget>[




                   DecoratedBox(
                       decoration: BoxDecoration(
                           border: new Border.all(color: Colors.black),
                           borderRadius: BorderRadius.circular(5.0)),
                       child: Padding(
                         padding: EdgeInsets.fromLTRB(10, 5, 0, 0),
                         //Why you have used Stack ??????
                         //B'coz it make clickable to whole decorated Box!!!! as you can click anywhere for dropdown !!!
                         child: Stack(
                           children: <Widget>[

//Country Text
                             Text(
                               "Country:  ",

                               style: TextStyle(
                                 fontSize: 13.0,
                               ),
                             ),

//Dropdown that has no loine beneath

                             DropdownButtonHideUnderline(

                               child:

//starting the dropdown
                               DropdownButton(
                                 items: country_data.map((item) {

                                   return new DropdownMenuItem(

                                       child: new Text(
                                         item['countries']['name'],    //Names that the api dropdown contains
                                         style: TextStyle(
                                           fontSize: 13.0,
                                         ),
                                       ),
                                       value: item['countries']['id'].toString()


                                      //e.g   India (Name)    and   its   ID (55fgf5f6frf56f) somethimg like that....
                                   );
                                 }).toList(),

                                 onChanged: (String newVal)  {
                                   setState(() {
                                    countryid = newVal;
                                     print(countryid.toString());
                                   });
                                 },

                                 value: countryid,                 //pasing the default id that has to be viewed... //i havnt used something ... //you can place some (id)
                               ),


                             )
                           ],
                         ),
                       )),
                 ],
               ),

the json response from api server:


{
    "products": [],
    "productStatus": [
        {
            "name": "OLD",
            "id": 1
        }
    ],
    "countries": [
        {
            "name": "Turkey",
            "id": 1
        },
        {
            "name": "Syria",
            "id": 2
        }
    ],
    "currencies": [
        {
            "name": "DOLLAR",
            "id": 1
        }
    ],
    "units": [
        {
            "name": "KG",
            "id": 1
        },
        {
            "name": "liter",
            "id": 2
        }
    ],
    "addresses": []
}

I didn't get any error my code starting without error please try to help me and thank you very much any question just do comment

来源:https://stackoverflow.com/questions/62049329/i-want-to-display-data-in-dropdownbutton-from-rest-api-using-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!