必须为文本窗口小部件提供非空字符串

Mm *_*ory 4 dart flutter flutter-layout

我正在尝试添加要调整的数量的选项,但是出现错误消息“必须向文本小部件提供非空字符串”如何在此代码中提供此信息?https://i.stack.imgur.com/Qp8pO.png

 trailing: Container(
        height: 60,
        width: 60,
        padding: EdgeInsets.only(left: 10),
        child: Column(
          children: <Widget>[
            new GestureDetector(child: Icon(Icons.arrow_drop_up), onTap: () {}),
            new Text(cart_prod_qty),
            new GestureDetector(child: Icon(Icons.arrow_drop_down), onTap: () {})
          ],
        ),
Run Code Online (Sandbox Code Playgroud)

Pra*_*ndo 6

只需检查 null 并给出默认值

Text(cart_prod_qty!=null?cart_prod_qty:'default value'),
Run Code Online (Sandbox Code Playgroud)

如果你愿意,你可以把它留空

Text(cart_prod_qty!=null?cart_prod_qty:''),
Run Code Online (Sandbox Code Playgroud)

否则,您可以将文本小部件设为可选

cart_prod_qty!=null? Text(cart_prod_qty): Container()
Run Code Online (Sandbox Code Playgroud)


Nik*_*iya 6

您应该检查null安全

Text(cart_prod_qty??'default value'),
Run Code Online (Sandbox Code Playgroud)


jit*_*555 5

The error itself shows what's wrong in the code, Text widget works only with string and for null they intentionally have thrown an exception. Check text.dart file implementation where they added throwing an exception.

assert(
         data != null,
         'A non-null String must be provided to a Text widget.',
       ),
Run Code Online (Sandbox Code Playgroud)

To solve above error you have to provide some default text.

new Text(cart_prod_qty!=null?cart_prod_qty:'Default Value'),
Run Code Online (Sandbox Code Playgroud)