简体   繁体   English

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

[英]The following NoSuchMethodError was thrown building Home(): The method '>' was called on null. Receiver: null Tried calling: >(1)

ERROR:The following NoSuchMethodError was thrown building Home(dirty, dependencies: [MediaQuery], state: _HomeState#a6c1f): The method '>' was called on null.错误:在构建 Home(脏,依赖项:[MediaQuery],state:_HomeState#a6c1f)时引发了以下 NoSuchMethodError:在 null 上调用了方法“>”。 Receiver: null Tried calling: >(1)接收方:null 尝试呼叫:>(1)

i want to take multiple values in the second input box and the no of inputs are user dependant so i made a function to return the container with the text form field each time store the value in a different array index when i go that error我想在第二个输入框中取多个值,并且输入的数量取决于用户所以我做了一个 function 来返回带有文本表单字段的容器,每次将值存储在不同的数组索引中时我 go 那个错误

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

see if this works for you.看看这是否适合你。 Create another variable heightFields to store all your textfields创建另一个变量 heightFields 来存储所有文本字段

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

write a function to fill the variable写一个 function 来填充变量

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);
            });
          },
        ),
      ));
    }
}

Then in your on submitted field, call the function然后在您提交的字段中,调用 function

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

and in the place between the Row widgets where you call getinput() replace it with a column并在您调用 getinput() 的 Row 小部件之间的位置将其替换为一列

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

When flutter runs your app for the first time, the value for buildings is null(from your program buildings is assigned a value after your form is submitted onFormSubmitted).当 flutter 首次运行您的应用程序时,建筑物的值为 null(在您的表单提交 onFormSubmitted 后,您的程序为建筑物分配了一个值)。 but the widget getInput uses the parameter for buildings before it is assigned a value.但是小部件 getInput 在分配值之前使用建筑物的参数。

Assign a value for buildings when it is declared to solve problem在声明解决问题时为建筑物赋值

int buildings = 1;
List<int> heights = [];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 null 上调用了方法“[]”。 接收方:null 尝试调用:[](“日期时间”实例) - The method '[]' was called on null. Receiver: null Tried calling: [](Instance of 'DateTime') 在 null 上调用了方法“toDouble”。 接收方:null 尝试调用:toDouble() - The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble() Flutter:NoSuchMethodError:在 null 上调用了 getter 'nom'。 接收者; null 尝试调用:nom - Flutter : NoSuchMethodError: The getter 'nom' was called on null. Receiver; null Tried calling: nom NoSuchMethodError:在 null 上调用了 getter 'path'。 接收方:null 尝试调用:路径 - NoSuchMethodError: The getter 'path' was called on null. Receiver: null Tried calling: path Flutter 异常:在 null 上调用了方法“forEach”。 接收方:null 尝试调用:forEach(Closure: (CarModel) =&gt; Null) - Flutter exception: The method 'forEach' was called on null. Receiver: null Tried calling: forEach(Closure: (CarModel) => Null) 在构建 Builder 时抛出了以下 NoSuchMethodError:getter &#39;email&#39; 在 null 上被调用。 在火力基地 - The following NoSuchMethodError was thrown building Builder: The getter 'email' was called on null. in firebase 在 null 上调用了 getter 'snapshot'。 接收方:null 尝试调用:snapshot - The getter 'snapshot' was called on null. Receiver: null Tried calling: snapshot Flutter 问题:在 null 上调用了方法“[]”。 接收方:null 尝试调用:[](0),“错误” - Flutter Problem : The method '[]' was called on null. Receiver: null Tried calling: [](0) , “Error” 如何修复在 null 上调用了方法“&gt;=”。 接收方:null 尝试调用:&gt;=(25) in dart flutter - How to Fix The method '>=' was called on null. Receiver: null Tried calling: >=(25) in dart flutter 我正面临一个错误在 null 上调用了方法 '[]'。接收方:null 尝试调用:[]("postId") - I'm facing a error The method '[]' was called on null. Receiver: null Tried calling: []("postId")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM