1

I am currently building a botcoin ticker app that returns a list of currencies to a dropDown menu . I am getting the following errors:-

error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. (body_might_complete_normally at [bitcoin_ticker] lib\price_screen.dart:10)

error: The argument type 'List<DropdownMenuItem>' can't be assigned to the parameter type 'List<DropdownMenuItem>?'. (argument_type_not_assignable at [bitcoin_ticker] lib\price_screen.dart:60)

Here is my code

import 'package:flutter/material.dart';
import 'coin_data.dart';
int i = 0;
class PriceScreen extends StatefulWidget {
  @override
  _PriceScreenState createState() => _PriceScreenState();
}


List<DropdownMenuItem> getDropDownItems() {

  List<DropdownMenuItem<String>> dropdownItem = [];
  for (String currency in currenciesList) {
     DropdownMenuItem(
      child: Text(currency), //
      value: currency,
    );
  }
}
class _PriceScreenState extends State<PriceScreen> {
  late String selectedCurrency = 'USD';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(' Coin Ticker'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(18.0, 18.0, 18.0, 0),
            child: Card(
              color: Colors.lightBlueAccent,
              elevation: 5.0,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0),
              ),
              child: Padding(
                padding: EdgeInsets.symmetric(vertical: 15.0, horizontal: 28.0),
                child: Text(
                  '1 BTC = ? USD',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                ),
              ),
            ),
          ),
          Container(
            height: 150.0,
            alignment: Alignment.center,
            padding: EdgeInsets.only(bottom: 30.0),
            color: Colors.lightBlue,
            child: DropdownButton<String>(
              value:selectedCurrency,//Default value
              items: getDropDownItems(),
             onChanged:(value){//it is like an on Pressed Button
                setState(() {
                  selectedCurrency = value!;
                  print(selectedCurrency);
                });
             },
            ),
          ),
        ],
      ),
    );
  }

}
Karthik KK
  • 157
  • 1
  • 11
  • Your `getDropDownItems` function is missing a `return` statement and therefore does not return anything. Additionally, its `for`-loop body does not do anything with the `DropdownMenuItem` objects it constructs. – jamesdlin Sep 22 '21 at 02:57
  • @jamesdlin I wrote the for loop to access the list and print it in the drop down..Can u pls tell how I can change the for loop – Karthik KK Sep 22 '21 at 04:51

1 Answers1

0

The warning shows because the getDropDownItems doesn't returning anything from the function. Also you are not updating the list in the for loop. you can refer the following code

  List<DropdownMenuItem> getDropDownItems() {
    List<DropdownMenuItem> dropdownItem = currenciesList
        .map((currency) => DropdownMenuItem(
              child: Text(currency), //
              value: currency,
            ))
        .toList();

    return dropdownItem;
  }
Nidheesh MT
  • 1,074
  • 11
  • 18