如何解决:未处理的异常:类型'SocketException'不是'字符串'类型的子类型?

时间:2020-04-18 20:03:28

标签: flutter dart

此函数会产生错误:

static Future<String> createTable() async {
try {
  var map = Map<String, dynamic>();
  map['action'] = 'CREATE_TABLE';
  final response = await http.post(ROOTurl, body: map);
  print('Create Table Response: ${response.body}');
  if (200 == response.statusCode) {
    return response.body;
  } else {
    return "error";
  }
} catch (e) {
  return "error"+e;
}}

完全例外:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'SocketException' is not a subtype of type 'String'

有什么办法可以解决这个问题?

1 个答案:

答案 0 :(得分:1)

您正在尝试将非字符串类型的String连接起来

尝试一下:

  try {
    //code throwing exceptions
  } on SocketException catch (e) {
    print('error ${e.message}');
  } catch (e) {
    //for other errors
    print('error ${e.toString()}');
  }