简体   繁体   English

DropdownButton 未显示在 Flutter 应用程序的第二页中

[英]DropdownButton not showing up in second page of flutter app

My DropdownButton will not show up on the second page of flutter app.我的 DropdownButton 不会显示在 flutter 应用程序的第二页上。

This is the DropdownButton code implemented in ConversionRates class (page2):这是在 ConversionRates 类(第 2 页)中实现的 DropdownButton 代码:

Widget buildDropDownButton(String currencyCategory) {
return DropdownButton(
    value: currencyCategory,
    items: converter.currencies.keys
        .map((String value) => DropdownMenuItem(
            value: value,
            child: Row(children: <Widget>[
              Text(value),
            ])))
        .toList(),
    onChanged: (String value) {
      if (currencyCategory == converter.fromCurrency) {
        setState(() {
          converter.onFromChanged(value);
        });
      } else {
        setState(() {
          converter.onToChanged(value);
        });
      }
    });

This is the code of the rest of the class ConversionRates (page2):这是类 ConversionRates(第 2 页)的其余部分的代码:

class ConversionRates extends StatefulWidget {
Map<String, double> currencies;

ConversionRates({Key key, @required this.currencies}) : super(key: key);

@override
ConversionRatesState createState() => ConversionRatesState(currencies);
}

class ConversionRatesState extends State<ConversionRates> {
final Converter converter = new Converter.defaultConstructor();

String _currentItemSelected = "SEK";

Map<string, double> currencies;

ConversionRatesState(this.currencies);

@override
void initState() {
  super.initState();

  setState(() {});
}

@override
Widget build(BuildContext context) {
  return Scaffold(
      backgroundColor: Colors.blueAccent,
      appBar: AppBar(
        backgroundColor: Colors.black87,
        title: Center(
            child: Text(
          "Conversion Rates",
          style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
              fontStyle: FontStyle.italic,
              fontSize: 35.0),
        )),
      ),
      body: converter.currencies.keys == null
          ? Center(child: CircularProgressIndicator())
          : Stack(children: [
              Container(
                  color: Colors.black26,
                  alignment: Alignment.center,
                  height: MediaQuery.of(context).size.height,
                  width: MediaQuery.of(context).size.width,
                  child: Center(
                      child: Stack(children: [
                    Positioned(
                        top: 40,
                        left: 70,
                        child: Container(
                            color: Colors.transparent,
                            child: Column(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceEvenly,
                                children: [
                                  ListTile(
                                      title: TextField(),
                                      trailing: buildDropDownButton(
                                          converter.fromCurrency)),
                                  RichText(
                                      text: TextSpan(
                                    children: <TextSpan>[
                                      TextSpan(
                                          text: "SEK: " +
                                              converter.currencies["SEK"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                      TextSpan(
                                          text: "\n\nUSD: " +
                                              converter.currencies["USD"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                      TextSpan(
                                          text: "\n\nEUR: " +
                                              converter.currencies["EUR"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                      TextSpan(
                                          text: "\n\nGBP: " +
                                              converter.currencies["GBP"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                      TextSpan(
                                          text: "\n\nCNY: " +
                                              converter.currencies["CNY"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                      TextSpan(
                                          text: "\n\nJPY: " +
                                              converter.currencies["JPY"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                      TextSpan(
                                          text: "\n\nKRW: " +
                                              converter.currencies["KRW"]
                                                  .toString(),
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 30.0)),
                                    ],
                                  ))
                                ])))
                  ])))
            ]));
}

The data I am receiveng from page1 is a map<string,double> currencies.我从 page1 收到的数据是 map<string,double> 货币。 The thing is that when I dont have a dropdownbutton on the second page I can access the data in the currencies map.问题是,当我在第二页上没有下拉按钮时,我可以访问货币地图中的数据。 So the data transfer is not the problem here.所以这里的数据传输不是问题。 But as soon as I try to make a DropdownButton I cant access the data in the currencies anymore.但是,一旦我尝试制作 DropdownButton,我就无法再访问货币中的数据。 I also dont get any dropdownbutton.我也没有得到任何下拉按钮。 I have tried solving this problem in different ways but I am ending up with either not have anything showing in the second page (except the bar and scaffold), like the Textspan or DropdownButton or I am ending upp with emulator error saying:我尝试以不同的方式解决这个问题,但我最终要么没有任何显示在第二页(除了栏和脚手架),如 Textspan 或 DropdownButton 要么我结束了模拟器错误说:

NoSuchMethodError: The getter 'keys' was called on null. NoSuchMethodError:在 null 上调用了 getter 'keys'。

So I think that the problem may be with the state managment but I dont really know how to manage it correctly.所以我认为问题可能出在状态管理上,但我真的不知道如何正确管理它。 I am also creating an instance converter so I can access some data from Converter class.我还创建了一个实例转换器,以便我可以从 Converter 类访问一些数据。 I also tried changing currencies map to map<dynamic,dynamic> currencies but that didnt solve the problem.我还尝试将货币映射更改为映射 <dynamic,dynamic> 货币,但这并没有解决问题。

Any ideas?有任何想法吗?

Your currency list of the converter is null too.您的转换器货币列表也为空。 You need to check it.你需要检查一下。 It will look like this:它看起来像这样:

     converter.currencies?.keys == null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM