Flutter DropdownButton 失败

Flutter DropdownButton is Failed

我在 appBar 和 Scaffold.The 的正文中创建了两个 DropdownButtons 每当它是 selected.After 我 selected 一个从第二个转换率下拉按钮和更改语言,代码crushes.Here是代码..

import 'package:flutter/material.dart';

 void main() => runApp(new MyApp());

 class MyApp extends StatelessWidget {

  @override
   Widget build(BuildContext context) {
    return new MaterialApp(
     title: 'Flutter Demo',
     theme: new ThemeData(
     primarySwatch: Colors.blue),
     home: new MyHomePage(title: 'Flutter Demo Home Page'),
     );
    }
   }

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
   final String title;

  @override
   _MyHomePageState createState() => new _MyHomePageState();
 }

   class User {
    User(this.name);
    final String name;
   }

   class CurrencyClass {
     final String currencyType;
     CurrencyClass(this.currencyType);
   }
   class _MyHomePageState extends State<MyHomePage> {

     User selectedUser;
     CurrencyClass selectedCurrency;
     List<User> users = <User> 
       [User('English'),User('Amharic'),User('Oromigna'),
       User('Tigrigna'),User('Somali')];
      List<CurrencyClass> currency = <CurrencyClass>[
          CurrencyClass('USD'),
          CurrencyClass('GBP'),
          CurrencyClass('CAD'),
          CurrencyClass('AUD'),
          CurrencyClass('EURO'),CurrencyClass('SAR'),];

     @override
      void initState() {
      super.initState();
      selectedUser = users[0];  
      }

   @override
     Widget build(BuildContext context) {
      return new Scaffold(
      appBar: new AppBar(
      title: new Text(widget.title),
      actions: <Widget>[ 
      DropdownButton<User>(
      value: selectedUser,

      onChanged:(User newValue){
        setState(() {
                      selectedUser = newValue;     

                      if(newValue == users[0]){

                     currency = <CurrencyClass>[CurrencyClass('English'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
                      }
                      else if(newValue == users[1]){
                     currency = <CurrencyClass>[CurrencyClass('Amharic'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
                      } 

                      else if(newValue == users[2]){
                      currency = <CurrencyClass>[CurrencyClass('Oromigna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];

                      } else if(newValue == users[3]){
                           currency = <CurrencyClass>[CurrencyClass('Tigringa'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];

                      } else {
                      currency = <CurrencyClass>[CurrencyClass('Somaligna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];

                      }

                    });
      },
      items: users.map((User user){
        return new DropdownMenuItem<User>(
           value: user,
           child:Text(
            user.name,
            style: TextStyle(color:  Colors.black),
          ),
        );
      }).toList(),
    )
    ],
  ),
  body: new Center(
    child: new Column(
      mainAxisAlignment: MainAxisAlignment.center,
       children: <Widget>[
        new Padding(padding: EdgeInsets.symmetric(vertical: 50.0),),
        new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Amount',style: TextStyle(fontSize: 20.0),),
            Padding(padding: EdgeInsets.symmetric(horizontal: 15.0)),
            Text('Birr',style: TextStyle(fontSize: 24.0),),
            Padding(padding: EdgeInsets.symmetric(horizontal: 15.0)),
            DropdownButton<CurrencyClass>(
             value: selectedCurrency,
             onChanged: (CurrencyClass newExchange){
               setState(() {
                 selectedCurrency = newExchange;
                          });                     
                },
             items: currency.map((CurrencyClass user2){
                return DropdownMenuItem<CurrencyClass>(
                 value: user2,
                 child: Text(
                   user2.currencyType,
                   style: TextStyle(color: Colors.red),
                 ),
                );
             }).toList(),
            ),
          ],
        ),
       ],
      ),
     ),
    );
   }
  }

这是我在 select 底部下拉按钮

中输入一些值后 select 顶部下拉按钮时的错误
Launching lib/main.dart on XT1068 in debug mode...
Built build/app/outputs/apk/debug/app-debug.apk.
I/FlutterActivityDelegate(11200): onResume setting current activity to 
this
I/flutter (11200): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY 
╞═══════════════════════════════════════════════════════════
I/flutter (11200): The following assertion was thrown building 
MyHomePage(dirty, state: _MyHomePageState#3ce11):
I/flutter (11200): 'package:flutter/src/material/dropdown.dart': F 
Failed assertion: line 481 pos 15: 'value == null ||
I/flutter (11200): items.where((DropdownMenuItem<T> item) => 
item.value == value).length == 1': is not true.
I/flutter (11200): Either the assertion indicates an error in the 
framework itself, or we should provide substantially
I/flutter (11200): more information in this error message to help you 
determine and fix the underlying cause.

原始 post 中的问题是在选择条形下拉菜单时设置货币值。您还需要设置 selectedCurrency.

selectedCurrency = currency[0];

或者像 Gunter 在编辑中所做的那样。只是不要更改货币下拉列表,所以根本没有问题。

    onChanged:(User newValue){
      setState(() {
        selectedUser = newValue;

        if(newValue == users[0]){
          currency = <CurrencyClass>[CurrencyClass('English'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
        }
        else if(newValue == users[1]){
          currency = <CurrencyClass>[CurrencyClass('Amharic'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
        }
        else if(newValue == users[2]){
          currency = <CurrencyClass>[CurrencyClass('Oromigna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
        }
        else if(newValue == users[3]){
          currency = <CurrencyClass>[CurrencyClass('Tigringa'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
        }
        else {
          currency = <CurrencyClass>[CurrencyClass('Somaligna'),CurrencyClass('CAD'),CurrencyClass('AUD'),CurrencyClass('EURO'),CurrencyClass('SAR')];
        }
        selectedCurrency = currency[0];
      });
    },