简体   繁体   中英

RangeError (index): Invalid value: Valid value range is 0

I want to accept the heights for user input number of buildings so I ask the user to enter the values in h[space]h format and then I extract the numbers and put them in heights list but when I display one of the indexes I get the above error|| import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  int buildings;
  List<int> heights = [];



  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: SingleChildScrollView(
          child: Container(
            width: MediaQuery.of(context).size.width,
            height: 341,
            decoration: BoxDecoration(
                gradient: LinearGradient(colors: [
                  Color.fromRGBO(223, 39, 17, 0.98),
                  Color.fromRGBO(245, 160, 25, 0.98)
                ], begin: Alignment.topRight, end: Alignment.bottomLeft),
                borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(30),
                    bottomRight: Radius.circular(30)),
                boxShadow: [
                  BoxShadow(
                      color: Colors.black12,
                      spreadRadius: 5,
                      blurRadius: 5,
                      offset: Offset(1, 2))
                ]),
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      SizedBox(
                        width: 278,
                      ),
                      CircleAvatar(
                          radius: 25,
                          backgroundColor: Colors.transparent,
                          backgroundImage: AssetImage("assets/icon2.png"))
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        "How many Buildings?",
                        style: TextStyle(
                          fontSize: 24,
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          //fontStyle: FontStyle.italic,
                          fontFamily: 'MuseoModerno',
                        ),
                      ),
                    ],
                  ),
                  Container(
                    width: 325,
                    height: 60,
                    child: TextFormField(
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(15)),
                        hintText: 'Enter Number of Buildings and press enter',
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                      ),
                      onFieldSubmitted: (String n) {
                        setState(() {
                          buildings = int.parse(n);
                        });
                      },
                    ),
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        "Enter the Heights",
                        style: TextStyle(
                          fontSize: 22,
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          //fontStyle: FontStyle.italic,
                          fontFamily: 'MuseoModerno',
                        ),
                      ),
                    ],
                  ),
                  Container(
                    width: 325,
                    height: 60,
                    child: TextFormField(
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(15)),
                        hintText:
                            'Enter heights in h[space]h[space]h[space]h... format',
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                      ),
                      onFieldSubmitted: (String n) {
                        setState(() {
                          for (int i = 0; i <= buildings - 1; i++) {
                            if (i != buildings - 1) {
                              if (n.substring(i, i + 1) != " ") {
                                heights[i] = int.parse(n.substring(i, i + 1));
                              }
                            } else {
                              heights[i]=int.parse(n.substring(i-1,i));
                            }
                          }
                        });
                      },
                    ),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.only(top: 1),
                        child: ButtonTheme(
                          height: 55,
                          minWidth: 65,
                          child: RaisedButton(
                            elevation: 3,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(30.0),
                                side: BorderSide(color: Colors.transparent)),
                            child: new Text(
                              "Initiate",
                              style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 25,
                                  fontFamily: "MuseoModerno",
                                  fontWeight: FontWeight.bold),
                            ),
                            color: Colors.black,
                            onPressed: () {
                              print(buildings);
                              print(heights[0]);
                            },
                          ),
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
        ));
  }
}

Your heights list is empty so you cannot use this to access the elements

heights[i] = int.parse(n.substring(i, i + 1));

Instead use heights.add() to add an element to the list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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