简体   繁体   English

文本小部件不得为 null

[英]Text widget must not be null

I pass argument through a pushNamedandRemoveUntil but it shows text widget must not be null when I try to access the passed parameter.我通过 pushNamedandRemoveUntil 传递参数,但当我尝试访问传递的参数时,它显示文本小部件不能是 null。

CustomButton(
                    buttonText: 'Go',
                    buttonClick: () {Navigator.pushNamedAndRemoveUntil(context, Next.id, (route) => false, arguments: "123456789");},
                  ),

class Next extends StatefulWidget {

  const Next({this.number});
  static const id = "next";
  final String number;
  @override
  _NextState createState() => _NextState();
}

class _NextState extends State<Next> {
 @override
  Widget build(BuildContext context) {
       return Scaffold(
body: Container(child: Center(child: Text(widget.number)))
)
}
}

Error is Failed assertion: line 298 pos 10: 'data != null'错误是断言失败:第 298 行 pos 10:'data != null'

its because your passed parameter is null这是因为您传递的参数是 null

and if you want to handle the error you should use Text widget like this:如果你想处理错误,你应该使用这样的 Text 小部件:

Text(widget.number ?? '')

and you should pass parameter like this你应该像这样传递参数

CustomButton(
  buttonText: 'Go',
  buttonClick: () {
      navigator.pushAndRemoveUntil(
         MaterialPageRoute(builder: (BuildContext context) => Next(id: '123456789')),
        (Route<dynamic> route) => false,
      );
),

You can't access like that.你不能这样访问。 Use ModalRoute in your next screen and fetch the data.在下一个屏幕中使用ModalRoute并获取数据。

Widget build(BuildContext context) {
      final number = ModalRoute.of(context).settings.arguments as String;
//....
}

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

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