flutter 类型'Null'不是类型'String'的子类型=>我已经在这上面工作了3天,请帮助我

hvvq6cgz  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(108)

当我运行这个项目模拟器给了我这样的错误“类型'Null'不是类型'字符串'的子类型”,我找不到问题,但我认为语法没有问题。有什么方法可以解决我的问题吗?我真实的的需要帮助。

Future<void> _asyncInputDialog(
  BuildContext context, String aimagId, String aimagName) async {
String? teamName;
return showDialog(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text(aimagName ?? 'Default Name'),
      content: Row(
        children: [
          Expanded(
            child: TextField(
              autofocus: true,
              decoration: InputDecoration(
                labelText: 'Баталгаажуулах код',
                hintText: 'код оруулна уу',
              ),
              onChanged: (value) {
                teamName = value.isNotEmpty ? value : null;
              },
            ),
          ),
        ],
      ),
      actions: [
        TextButton(
          child: Text('ХААХ'),
          onPressed: () {
            Get.back();
          },
        ),
        TextButton(
          child: Text('OK'),
          onPressed: () async {
            await controller.getConnectionData(
              aimagId,
              teamName ?? 'String Value is Null',
              '',
            );
            if (controller.status == "1") {
              Get.back();
            }
          },
        ),
      ],
    );
  },
);

}

wqnecbli

wqnecbli1#

试试下面的代码

Future<void> _asyncInputDialog(
  BuildContext context, String aimagId, String aimagName) async {
String? teamName;
return showDialog(
  context: context,
  barrierDismissible: false,
  builder: (BuildContext context) {
    return AlertDialog(
      title: Text(aimagName.isEmpty ? 'Default Name' : aimagName),
      content: Row(
        children: [
          Expanded(
            child: TextField(
              autofocus: true,
              decoration: const InputDecoration(
                labelText: 'Баталгаажуулах код',
                hintText: 'код оруулна уу',
              ),
              onChanged: (value) {
                teamName = value.isNotEmpty ? value : null;
              },
            ),
          ),
        ],
      ),
      actions: [
        TextButton(
          child: Text('ХААХ'),
          onPressed: () {
            Get.back();
          },
        ),
        TextButton(
          child: Text('OK'),
          onPressed: () async {
            await controller.getConnectionData(
              aimagId.isEmpty ? "DefaultID" : aimagId,
              teamName ?? 'String Value is Null',
              '',
            );
            if (controller.status == "1") {
              Get.back();
            }
          },
        ),
      ],
    );
  },
);
}

如果aimagId为空,则需要提供default-Id。

相关问题