flutter_http_logger 0.0.2 copy "flutter_http_logger: ^0.0.2" to clipboard
flutter_http_logger: ^0.0.2 copied to clipboard

A Flutter package to log HTTP requests and responses for debugging and monitoring purposes. This package provides an easy way to intercept and log network traffic, making it easier to understand and d [...]

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_http_logger/flutter_http_logger.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'HTTP Logger Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HttpLoggerExample(),
    );
  }
}

class HttpLoggerExample extends StatefulWidget {
  const HttpLoggerExample({Key? key}) : super(key: key);

  @override
  _HttpLoggerExampleState createState() => _HttpLoggerExampleState();
}

class _HttpLoggerExampleState extends State<HttpLoggerExample> {
  String _response = "No response yet";

  @override
  void initState() {
    super.initState();
    // Start the HTTP Logger server
    HttpLog.startServer(context);
  }

  // Function to make a simple GET request
  Future<void> _makeGetRequest() async {
    var url = 'https://jsonplaceholder.typicode.com/posts/1';
    var headers = {"Content-Type": "application/json"};

    // Make the HTTP GET request
    var response = await http.get(Uri.parse(url), headers: headers);

    // Log the GET request and response
    HttpLog.sendLog(
      method: "GET",
      url: url,
      header: headers,
      statusCode: response.statusCode,
      response: response.body,
    );

    setState(() {
      _response = response.body;
    });
  }

  // Function to make a simple POST request
  Future<void> _makePostRequest() async {
    var url = 'https://jsonplaceholder.typicode.com/posts';
    var headers = {"Content-Type": "application/json"};
    var requestBody = '{"title": "foo", "body": "bar", "userId": 1}';

    // Make the HTTP POST request
    var response =
        await http.post(Uri.parse(url), headers: headers, body: requestBody);

    // Log the POST request and response
    HttpLog.sendLog(
      method: "POST",
      url: url,
      header: headers,
      request: requestBody,
      statusCode: response.statusCode,
      response: response.body,
    );

    setState(() {
      _response = response.body;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('HTTP Logger Example'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            ElevatedButton(
              onPressed: _makeGetRequest,
              child: const Text('Make GET Request'),
            ),
            ElevatedButton(
              onPressed: _makePostRequest,
              child: const Text('Make POST Request'),
            ),
            const SizedBox(height: 20),
            const Text('Response:'),
            Expanded(
              child: SingleChildScrollView(
                child: Text(_response),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
3
likes
0
points
130
downloads

Publisher

verified publishershuklashrestha.com.np

Weekly Downloads

A Flutter package to log HTTP requests and responses for debugging and monitoring purposes. This package provides an easy way to intercept and log network traffic, making it easier to understand and debug the interactions between your app and backend services.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on flutter_http_logger