无法从下拉文本字段中获取值

问题描述 投票:0回答:1

我正在使用 dropdown_textfield 包。我的问题是我无法从下拉菜单中获取字符串值。当我打印它时它不为空,但当我尝试插入时,它变成空。

下面是我的下拉菜单

Padding(
  padding: const EdgeInsets.symmetric(horizontal: 20),
  child: ClipRRect(
    borderRadius: BorderRadius.circular(10),
    child: DropDownTextField(
      controller: _cnt,
      clearOption: false,
      textFieldDecoration: InputDecoration(
        filled: true,
        fillColor: Colors.white,
        border: InputBorder.none,
        hintText: "Şehir seçiniz",
        hintStyle: GoogleFonts.inter(
          fontSize: 16,
          fontWeight: FontWeight.w300,
          color: Colors.white
        )
      ),
      validator: (value) {
        if (value==null) {
          return "Zorunlu alan";
        }else {
          return null;
        }
      },
      dropDownItemCount: 6,
      dropDownList: const [
        DropDownValueModel(name: 'İstanbul', value: 'İstanbul'),
        DropDownValueModel(name: 'Ankara', value: 'Ankara')
      ], 
      onChanged: (val){
        setState(() {
          selectedCity = val;
          print("$val");
        });
      },
    ),
  ),
)

以下是我如何将图像上传到firestore数据库

Future<void> _insertSellerDetails() async {

    // Bilgiler eksiksiz ise verileri firestore a gönder
    if (_formKey.currentState!.validate()) {
      try {
        String userId = FirebaseAuth.instance.currentUser?.uid ?? "";
        if (userId.isNotEmpty) {
          List<String> imageUrls = await _uploadImagesToStorage();

          Map<String, dynamic> sellerDetails = {
            "sellerName": sellerName.text,
            "sellerLastName": sellerLastName.text,
            "sellerAge": sellerAge.text,
            "sellerHeight": sellerHeight.text,
            "sellerWeight": sellerWeight.text,
            "sellerDesc": sellerDesc.text,
            "city":selectedCity,
            "district":selectedDistrict,
            "imageUrls": imageUrls,
          };
          await FirebaseFirestore.instance
              .collection("Users")
              .doc(userId)
              .update({"sellerDetails": sellerDetails});
        }
      } catch (e) {
        log("error $e");
        // Show error message to the user
      }
    }
  }

我在插入之前尝试打印 selectedCity ,但似乎不为空

flutter firebase user-interface mobile drop-down-menu
1个回答
0
投票

在您的代码中缺少

value
。你必须像下面这样提及

Padding(
  padding: const EdgeInsets.symmetric(horizontal: 20),
  child: ClipRRect(
    borderRadius: BorderRadius.circular(10),
    child: DropDownTextField(
      controller: _cnt,
      clearOption: false,
      value: selectedCity // mention value in dropdown
      textFieldDecoration: InputDecoration(
        filled: true,
        fillColor: Colors.white,
        border: InputBorder.none,
        hintText: "Şehir seçiniz",
        hintStyle: GoogleFonts.inter(
          fontSize: 16,
          fontWeight: FontWeight.w300,
          color: Colors.white
        )
      ),
      validator: (value) {
        if (value==null) {
          return "Zorunlu alan";
        }else {
          return null;
        }
      },
      dropDownItemCount: 6,
      dropDownList: const [
        DropDownValueModel(name: 'İstanbul', value: 'İstanbul'),
        DropDownValueModel(name: 'Ankara', value: 'Ankara')
      ], 
      onChanged: (val){
        setState(() {
          selectedCity = val;
          print("$val");
        });
      },
    ),
  ),
)
© www.soinside.com 2019 - 2024. All rights reserved.