DropdownButton not showing up in second page of flutter app

32 Views Asked by At

My DropdownButton will not show up on the second page of flutter app.

This is the DropdownButton code implemented in ConversionRates class (page2):

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):

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. 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. 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:

NoSuchMethodError: The getter 'keys' was called on null.

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. I also tried changing currencies map to map<dynamic,dynamic> currencies but that didnt solve the problem.

Any ideas?

1

There are 1 best solutions below

1
On

Your currency list of the converter is null too. You need to check it. It will look like this:

     converter.currencies?.keys == null