1

我的 DropdownButton 不会出现在 Flutter 应用的第二页上。

这是 ConversionRates 类 (page2) 中实现的 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);
        });
      }
    });

这是类 ConversionRates (page2) 其余部分的代码:

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

我从 page1 收到的数据是 map<string,double> 货币。问题是,当我在第二页上没有下拉按钮时,我可以访问货币地图中的数据。所以这里的数据传输不是问题。但是,一旦我尝试制作 DropdownButton,我就无法再访问货币中的数据了。我也没有得到任何下拉按钮。我已经尝试以不同的方式解决这个问题,但我最终要么没有在第二页中显示任何内容(除了栏和脚手架),比如 Textspan 或 DropdownButton,要么我以模拟器错误告终:

NoSuchMethodError:在 null 上调用了 getter 'keys'。

所以我认为问题可能出在状态管理上,但我真的不知道如何正确管理它。我还在创建一个实例转换器,这样我就可以从 Converter 类中访问一些数据。我还尝试将货币映射更改为 map<dynamic,dynamic> 货币,但这并没有解决问题。

有任何想法吗?

4

1 回答 1

0

您的转换器货币列表也是空的。你需要检查一下。它看起来像这样:

     converter.currencies?.keys == null
于 2020-11-22T13:47:06.847 回答