在构建Home()时引发了以下NoSuchMethodError:在null上调用了方法'>'。接收方:null尝试调用:>(1)

时间:2020-06-16 13:57:56

标签: android arrays function flutter dart

错误:构建Home(脏,依赖项:[MediaQuery],状态:_HomeState#a6c1f)时,引发了以下NoSuchMethodError: 方法'>'在null上被调用。 接收者:null 尝试调用:>(1)

我想在第二个输入框中获取多个值,并且输入的数量取决于用户,因此我创建了一个函数,该函数每次将值存储在不同的数组索引中时,将带有文本表单字段的容器返回错误

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

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

  Widget getinput() {
    for (var i = 1; i <= buildings; i++) {
      return Container(
        width: 325,
        height: 60,
        child: TextFormField(
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(15)),
            hintText: 'Enter height and press enter to type next',
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.white),
            ),
          ),
          onFieldSubmitted: (String n) {
            setState(() {
              heights[i] = int.parse(n);
            });
          },
        ),
      );
    }
  }

  @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',
                        ),
                      ),
                    ],
                  ),
                  getinput(),
                  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: () {},
                          ),
                        ),
                      )
                    ],
                  )
                ],
              ),
            ),
          ),
        ));
  }
}

2 个答案:

答案 0 :(得分:0)

看看这是否适合您。创建另一个变量heightFields以存储所有文本字段

int buildings;
List<int> heights = [];
List<Widget> heightFields = []; //this one

编写一个函数来填充变量

void addAllHeightFields(){
heightFields.clear();
//guess i should start from 0 and not 1
for (int i = 0; i <= buildings -1; i++) {
      heightFields.add( Container(
        width: 325,
        height: 60,
        child: TextFormField(
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(15)),
            hintText: 'Enter height and press enter to type next',
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(color: Colors.white),
            ),
          ),
          onFieldSubmitted: (String n) {
            setState(() {
              heights[i] = int.parse(n);
            });
          },
        ),
      ));
    }
}

然后在您提交的字段中,调用函数

 onFieldSubmitted: (String n) {
                        setState(() {
                          buildings = int.parse(n);
                          //maybe you should clear the heights list?
                          addAllHeightFields();  //this line
                        });
                      },

,在Row小部件之间的调用getinput()的地方,将其替换为一列

buildings != null ? Column(children: heightFields ): Container(),

答案 1 :(得分:0)

当flutter第一次运行你的app时,buildings的值为null(你的程序中的buildings在表单提交后被赋值)。 但是小部件 getInput 在分配值之前使用建筑物的参数。

当建筑物被宣布解决问题时为其赋值

int buildings = 1;
List<int> heights = [];
相关问题