簡體   English   中英

檢測到 0 個或 2 個或多個 [DropdownMenuItem] 具有相同的值斷言失敗:第 882 行 pos 15:'items == null || items.isEmpty

[英]Either zero or 2 or more [DropdownMenuItem]s were detected with the same value Failed assertion: line 882 pos 15: 'items == null || items.isEmpty

我嘗試了在同一個問題上提供的解決方案,但每個問題都有自己的情況,所以在我的情況下,我試圖創建國家列表的下拉列表,然后在 Scaffold 小部件中使用它之前,我想將其保存到類型的實例DropdownButton(),代碼如下:

 List<String> provinces = [
    'عمان',
    'إربد',
    'الزرقاء',
    'المفرق',
    'الطفيلة',
    'عجلون',
    'معان',
    'الكرك',
    'مادبا',
    'العقبة',
    'البلقاء',
    'جرش'
  ];


String dropDownValue = "";

final provincesField = DropdownButton<String>(
  value: dropDownValue,
  icon: Icon(Icons.keyboard_arrow_down),
  items: provinces.map((String items) {
    return DropdownMenuItem(value: items, child: Text(items));
  }).toList(),
  onChanged: (String? newValue) {
    setState(() {
      dropDownValue = newValue!;
    });
  },
);

我不確定為什么它不訪問列表並認為它是空的。 這是完整的錯誤:

The following assertion was thrown building RegistrationScreen(dirty, state: _RegistrationScreenState#b01ae):
There should be exactly one item with [DropdownButton]'s value: . 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
package:flutter/…/material/dropdown.dart:1
Failed assertion: line 882 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

錯誤:

There should be exactly one item with [DropdownButton]'s value: one. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value

表示在您的列表provinces中,它應該包含與您的dropDownValue相同的值。

這意味着,您的文件應如下所示:

String dropDownValue = "one";
  List<String> provinces = [
    "one", // <-- Notice we're adding the word "one" to the list since that's the value of `dropDownValue`
    'عمان',
    'إربد',
    'الزرقاء',
    'المفرق',
    'الطفيلة',
    'عجلون',
    'معان',
    'الكرك',
    'مادبا',
    'العقبة',
    'البلقاء',
    'جرش'
  ];

這是一個完整的例子:

class Test extends StatefulWidget {
  const Test({Key? key}) : super(key: key);

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {
  List<String> provinces = [
    "one", // <-- Notice wer're adding the word "one" to the list
    'عمان',
    'إربد',
    'الزرقاء',
    'المفرق',
    'الطفيلة',
    'عجلون',
    'معان',
    'الكرك',
    'مادبا',
    'العقبة',
    'البلقاء',
    'جرش'
  ];

  @override
  Widget build(BuildContext context) {

    // make sure this is this value is in your list
    String dropDownValue = "one";

    final provincesField = DropdownButton<String>(
      value: dropDownValue,
      icon: Icon(Icons.keyboard_arrow_down),
      items: provinces.map((String items) {
        return DropdownMenuItem(value: items, child: Text(items));
      }).toList(),
      onChanged: (String? newValue) {
        setState(() {
          dropDownValue = newValue!;
        });
      },
    );

    return Container();
  }
}

嘗試替換代碼而不是 String dropDownValue。

var dropDownValue;

並且項目應該有一個非空值。

in constructor (){
   dropDownValue = provinces.first;
}

確保一個值在您的列表中

暫無
暫無

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

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