AHMED Gamal
AHMED Gamal

Reputation: 73

RangeError (index) : invalid calue : not in incusive range 0..2:3

hey guys I'm trying to make an app that will track bitcoin prices in a sample way ... but it give me that error in the android-emulator but nothing in the code itself and I don't know what went wrong,

I think that the problem is in the DropdownMenuItem list but I can't get it

can anyone help, thanks

class PriceScreen extends StatefulWidget {
  @override
  _PriceScreenState createState() => _PriceScreenState();
}

class _PriceScreenState extends State<PriceScreen> {
  String selectedCurrency = 'EGP';
  List<DropdownMenuItem<String>> getDropdownItems() {
    List<DropdownMenuItem<String>> dropdownItems = [];

    for (int i = 0; i < currenciesList.length; i++) {
      String currency = (cryptoList[i]);
      var newItem = DropdownMenuItem(
        child: Text(currency),
        value: currency,
      );
      dropdownItems.add(newItem);
    }
    return dropdownItems;
  }

  @override
  Widget build(BuildContext context) {
    getDropdownItems();
    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,
                items: getDropdownItems(),
                onChanged: (value) {
                  setState(() {
                    selectedCurrency = value!;
                  });
                }),
          ),
        ],
      ),
    );
  }
}

android emulator : enter image description here

Upvotes: 0

Views: 19

Answers (1)

Shri Hari L
Shri Hari L

Reputation: 4894

for loop should check for the length of the array you are accessing.

Hence, it should be either this

 for (int i = 0; i < currenciesList.length; i++) {
      String currency = (currenciesList[i]);
 }

or

for (int i = 0; i < cryptoList.length; i++) {
      String currency = (cryptoList[i]);
 }

Upvotes: 2

Related Questions