簡體   English   中英

為什么在將 DropdownButton 與 selectedItemBuilder 和提示一起使用時會出現類型錯誤?

[英]Why do I get a type error in flutter while using DropdownButton with selectedItemBuilder and hint?

同時使用 selectedBuilder 和提示時,我收到以下錯誤:

類型“DefaultTextStyle”不是“值”的“文本”類型的子類型

以下是我在代碼中使用的代碼片段:

final Map<String, String> _items = [
    {
      'name': "India",
      'code': "+91",
    },
  ];
Map<String, String> _seletedValue;

DropdownButtonFormField(
                      selectedItemBuilder: (_) {
                        return _items
                            .map(
                              (country) => Text(
                                "${country['name']} (${country['code']})",
                                style: TextStyle(
                                  color: Colors.black,
                                ),
                              ),
                            )
                            .toList();
                      },
                      dropdownColor: Color(0xFF4C4F89),
                      style: TextStyle(
                        color: Colors.white,
                      ),
                      items: _items
                          .map(
                            (country) => DropdownMenuItem(
                              child: Text(
                                "${country['name']} (${country['code']})",
                                style: TextStyle(
                                  color: Colors.white,
                                ),
                              ),
                              value: country,
                            ),
                          )
                          .toList(),
                      onChanged: (value) {
                        setState(() {
                          _seletedValue = value;
                        });
                      },
                      hint: Text('Select your country'),
                    )

有人可以建議一些更正來解決這個錯誤嗎?

我認為您需要將列表項轉換為小部件

selectedItemBuilder: (_) {
            return _items
                .map<Widget>(           // Casting
                  (country) => Text(
                    "${country['name']} (${country['code']})",
                    style: TextStyle(
                      color: Colors.black,
                    ),
                  ),
                )
                .toList();
          },

希望有效!
參考: https : //api.flutter.dev/flutter/material/DropdownButton/selectedItemBuilder.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM