tryParse static method

HttpMethod? tryParse(
  1. String? value
)

从字符串解析 HttpMethod

大小写不敏感,返回 null 表示未知方法。 示例:

HttpMethod.tryParse('GET')     // HttpMethod.get
HttpMethod.tryParse('post')    // HttpMethod.post
HttpMethod.tryParse(null)      // null
HttpMethod.tryParse('')        // null
HttpMethod.tryParse('UNKNOWN') // null

Implementation

static HttpMethod? tryParse(String? value) {
  if (value == null) return null;
  final v = value.trim();
  if (v.isEmpty) return null;

  switch (v.toUpperCase()) {
    case 'GET':
      return HttpMethod.get;
    case 'POST':
      return HttpMethod.post;
    case 'PUT':
      return HttpMethod.put;
    case 'DELETE':
      return HttpMethod.delete;
    case 'PATCH':
      return HttpMethod.patch;
    case 'HEAD':
      return HttpMethod.head;
    case 'OPTIONS':
      return HttpMethod.options;
    default:
      return null;
  }
}