【问题标题】:RangeError (index): Invalid value: Valid value range is 0RangeError(索引):无效值:有效值范围为0
【发布时间】:2020-10-07 00:15:40
【问题描述】:

我想接受用户输入建筑物数量的高度,所以我要求用户以 h[space]h 格式输入值,然后我提取数字并将它们放入高度列表中,但是当我显示其中一个时索引我得到上述错误|| 导入“包:颤振/material.dart”; 导入'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]);
                            },
                          ),
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
        ));
  }
}

【问题讨论】:

  • 在哪一行给出了这个错误?
  • 您应该添加完整的错误堆栈跟踪。
  • 改进了格式和拼写。

标签: android flutter user-interface button dart


【解决方案1】:

你的身高列表是空的,所以你不能用它来访问元素

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

改为使用heights.add() 将元素添加到列表中

【讨论】:

  • 当您执行 heights[i] 时,您正在访问列表中索引 i 处的数据,但是由于没有数据,您不能使用 heights[i]
猜你喜欢
  • 2021-06-16
  • 2019-07-25
  • 2021-10-06
  • 2021-11-13
  • 2021-01-01
  • 2020-06-06
  • 1970-01-01
  • 2019-12-01
  • 2021-07-31
相关资源
最近更新 更多