处理HTTP访问异常

1497次阅读  |  发布于5年以前

处理HTTP访问异常

An HTTP request may return a response, or it may generate an error.

import 'package:http/http.dart' as http;

handleSuccess(http.Response response) {
  print('something went right!');
  print(response.body);
}

handleFailure(error) {
  print('Something went wrong.');
  print(error.message);
}

main() async {
  try {
    var response = await http.get("http://some_bogus_website.org");
    handleSuccess(response);
  } catch (e) {
    handleFailure(e);
  }
}

Copyright© 2013-2019

京ICP备2023019179号-2