You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter扫码页面报错:type 'String' is not a subtype of type 'Null'

问题排查与解决

错误原因

你在_UserTelaState类里定义了一个返回null的code getter:

get code => null;

而在build方法的Text组件中直接使用了code,此时调用的是这个返回Null的getter,但Text组件要求传入String类型,导致类型不匹配报错。同时你在UserTela这个StatefulWidget里已经正确声明了final String code参数,该参数需要通过widget.code来访问。

修复步骤

  • 删除_UserTelaState类中的get code => null;这一行冗余代码。
  • 将build方法里的Text(code, ...)修改为Text(widget.code, ...),通过widget前缀访问StatefulWidget传入的code参数。

修复后的关键代码片段:

class _UserTelaState extends State<UserTela> {
  String? _selected;
  List<Map> _myJson = [
    {'id': '1', 'image': 'assets/icons/brasil.png', 'name': 'PT-BR'},
    {'id': '2', 'image': 'assets/icons/germany.png', 'name': 'DE'},
    {'id': '3', 'image': 'assets/icons/ukunited.png', 'name': 'US'}
  ];

  @override
  Widget build(BuildContext context) {
    // ...
    Text(
      widget.code,
      style: TextStyle(
        fontSize: 16,
        color: Colors.black87,
        letterSpacing: 1,
      ),
    ),
    // ...
  }
}

内容的提问来源于stack exchange,提问作者Caio L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 04:39:34