【问题标题】:How to fix a non-null String must be provided to a Text widget如何修复非空字符串必须提供给文本小部件
【发布时间】:2021-08-29 17:22:51
【问题描述】:

这是我的代码的一部分,我不断收到非空字符串必须提供给文本小部件。 还是新手,所以我不确定如何解决这个问题。我尝试将 ??"" 放在 child: Text(myQuiz[0][i.toString()] 上,但随后它给了我一个错误 The method '[]' was called on null。 接收方:空 尝试调用:

  Widget optionButton(String k) {
    return Padding(
      padding: EdgeInsets.only(
        top: 10
      ),
      child: MaterialButton(
        onPressed: () => checkAns(k),
        child: Text(
          myQuiz[1][i.toString()][k],
          style: TextStyle(
            color: Colors.black,
            fontFamily: "Open Sans",
            fontSize: 16.0,
          ),
          maxLines: 1,
        ),
        color: buttonColor[k],
        minWidth: 200.0,
        height: 45.0,
        shape:
        RoundedRectangleBorder(borderRadius: BorderRadius.circular(18.0),
            side: BorderSide(color: Color(0xffb0dab9))),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
    return WillPopScope(
      onWillPop: () {
        return showDialog(
            context: context,
            builder: (context) => AlertDialog(
              content: Text("You must finish this quiz :)"),
              actions: <Widget>[
                FlatButton(
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                  child: Text(
                    'Ok',
                  ),
                )
              ],
            ));
      },
      child: Scaffold(
        appBar: AppBar(title: Text('Quiz'),backgroundColor: Color(0xffb0dab9)),
        backgroundColor: Colors.yellow[100],
        body: Container(
        padding: EdgeInsets.all(50),
        child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
               Container(
                 padding: EdgeInsets.all(20),
                alignment: Alignment.center,
                child: Text(myQuiz[0][i.toString()],
                  style: TextStyle(fontSize: 20.0, color: Colors.black),
                ),
              ),
             AbsorbPointer(
                absorbing: disableAnswer,
                child: Container(
                  padding: EdgeInsets.only(top: 30),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      optionButton('a'),
                      optionButton('b'),
                      optionButton('c'),
                      optionButton('d'),
                    ],
                  ),
                ),
              ),
            Container(
              padding: EdgeInsets.only(top: 30),
              alignment: Alignment.topCenter,
              child: Center(
                child: Text(
                  showTimer ,
                  style: TextStyle(
                    fontSize: 20.0,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    ),
      )
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这是因为 null value 正在提供给您的 Text 小部件

    根据您的代码.. 以下一项或多项是 null不是字符串

    myQuiz[1][i.toString()][k]
    showTimer
    

    试试

    ...
    @override
    Widget build(BuildContext context) {
      print(myQuiz[1][i.toString()][k]);
      print(showTimer);
    

    找出哪个是空值

    【讨论】:

    • 我认为 (myQuiz[1][i.toString()][k]);是问题,我该如何解决这是我现在得到的错误在null上调用了方法'[]'。接收者:null 尝试调用:[]("a")
    【解决方案2】:

    试试这个。

    Text(
              myQuiz[1]?[i?.toString()]?[k] ?? "Default Value",
              style: TextStyle(
                color: Colors.black,
                fontFamily: "Open Sans",
                fontSize: 16.0,
              ),
              maxLines: 1,
            ),
    

    【讨论】:

    • 我放不下?在 [1] 和 ?在tostring之后
    【解决方案3】:

    感谢帮助,我找到了答案,在getRandom中,因为json没有0,所以当测验运行并打到0时,会导致错误

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2021-05-11
      • 2020-08-10
      • 2020-12-07
      • 2020-07-09
      • 2021-10-11
      • 2021-07-19
      • 2022-11-17
      相关资源
      最近更新 更多