0

I'm working on a Flutter project where I make multiple API calls using the http package. Right now, I'm writing separate get and post methods in different files, which is causing code duplication and inconsistency.

I want to build a single, reusable class that:

Manages a base URL

Handles GET and POST requests

Supports optional headers and JSON encoding

Returns parsed responses

Can be extended easily to support other HTTP methods

2
  • 1
    This is an opinion based question and there isn't any specific method for it every is handling it based on their choice and convenience I have created a few classes which I am using in most of my projects but in that I am using dio package if you want I can share it. Commented May 30 at 11:51
  • 1
    Sounds like a good plan to centralize that logic. Go for it! 👍 Commented May 30 at 14:22

1 Answer 1

0

Add http dependency to pubspec.yaml

dependencies:
  http: ^0.13.6

2. Create a file: api_service.dart

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

class ApiService {
  final String baseUrl;

  ApiService({required this.baseUrl});

  Future<dynamic> get(String endpoint, {Map<String, String>? headers}) async {
    final url = Uri.parse('$baseUrl$endpoint');
    try {
      final response = await http.get(url, headers: headers);
      return _handleResponse(response);
    } catch (e) {
      throw Exception('GET request error: $e');
    }
  }

  Future<dynamic> post(String endpoint, {Map<String, String>? headers, dynamic body}) async {
    final url = Uri.parse('$baseUrl$endpoint');
    try {
      final response = await http.post(
        url,
        headers: headers ?? {'Content-Type': 'application/json'},
        body: json.encode(body),
      );
      return _handleResponse(response);
    } catch (e) {
      throw Exception('POST request error: $e');
    }
  }

  dynamic _handleResponse(http.Response response) {
    final statusCode = response.statusCode;
    final body = response.body.isNotEmpty ? json.decode(response.body) : null;

    if (statusCode >= 200 && statusCode < 300) {
      return body;
    } else {
      throw Exception('Error ${response.statusCode}: ${response.reasonPhrase}');
    }
  }
}

3. Usage Example:

void main() async {
  final api = ApiService(baseUrl: 'https://jsonplaceholder.typicode.com/');

  // GET request
  try {
    final posts = await api.get('posts');
    print(posts);
  } catch (e) {
    print(e);
  }

  // POST request
  try {
    final newPost = await api.post('posts', body: {
      'title': 'foo',
      'body': 'bar',
      'userId': 1,
    });
    print(newPost);
  } catch (e) {
    print(e);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.