Flutter DropdownButton失败

时间:2018-06-13 14:42:35

标签: dart flutter flutter-layout raddropdownbutton

我在appBar和Body of a Scaffold中创建了两个DropdownButton。无论何时选择语言,Top DropdownButton都会更改语言。在我从第二个Dropdown按钮选择One Conversion rate并更改语言后,代码会崩溃。这是代码..

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(),
            ),
          ],
        ),
       ],
      ),
     ),
    );
   }
  }

在底部dropdownButton中选择一些值后选择顶部DropdownButton时出现错误

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.

1 个答案:

答案 0 :(得分:1)

原始帖子中的问题是在选择条形下拉列表时设置货币值。您还需要设置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];
      });
    },