首页 > 解决方案 > Flutter:如何在 Country Pickers DropDown 库中设置值

问题描述

我在设置 country_code_picker 和 country_state_city_picker 库的值时遇到问题。我尝试在编辑选项中设置值,这是我之前在创建新广告时选择的,但它不起作用。也许有人有与他们合作的经验,或者对我需要做什么有一些建议。

Firestore 中的数据。由于某种原因,Contry 代码值为空

这个问题在应用程序中的外观如何,您如何查看这些选择器中的数据未设置

新广告创建时的代码

 String countryvalue = '';
  String countrycode = '';
  String cityvalue = '';
  String statevalue = '';

  // Adding new ad
  Future newAdd() async{
    return ads.add({
      'title' : titlecontroller.text,
      'location': '$countryvalue,$cityvalue',
      'currency' : currentcurreny,
      'country' : countryvalue,
      'countrycode' : countrycode,
      'city' : cityvalue,
      'state' : statevalue,
      'phone' : phonecontroller.text,
      'price' : pricecontroller.text,
      'desc'  : desccontroller.text,
      'url1'  : widget.url,
      'url2'  : widget.url2,
      'url3'  : widget.url3,
      'category'  : categoryCurrentItem,
      'userid'  : user != null? user!.uid : googleUser!.id,
      'time'   : Timestamp.now()
    });

  }


  //  Using country_code_picker library
       CountryCodePicker(
       initialSelection: 'EN',
       onChanged: (CountryCode country){
       setState(() {
       countrycode = country.dialCode!;
        });
            },
               )

  // Using country_state_city_picker library
  SelectState(
  onCountryChanged: (String value) {
  setState(() {
  countryvalue = value;
   });
       },
  onCityChanged: (String value) {
  cityvalue = value;
   },
  onStateChanged: (String value) {
  setState(() {
  statevalue = value;
   });
     })

编辑时的代码并尝试设置保存的数据

 String? country;
  String? city;
  String? state;
  String? countrycode;


   //  Using country_code_picker library
   CountryCodePicker(
   initialSelection: widget.countrycode,
   onChanged: (CountryCode country){
   setState(() {
   widget.countrycode = country.dialCode!;
        });
            },
                )
   // Using country_state_city_picker library
   SelectState(
    onCountryChanged: (String value) {
    setState(() {
    widget.country = value;
          });
              },  
    onCityChanged: (String value) {
      setState(() {
      widget.city = value;
           });

              },
    onStateChanged: (String value) {
      setState(() {
      widget.state = value;
            });
            },)

标签: flutterdart

解决方案


推荐阅读