skills/flutter/flutter-use-http-package/SKILL.md
Perform REST API networking operations (GET, POST, PUT, DELETE) using the lightweight and robust standard `http` package, including platform configurations and background parsing models.
npx skillsauth add dhruvanbhalara/skills flutter-use-http-packageInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Configure the development environment and platform-specific access controls to enable network requests:
http dependency using your terminal:
flutter pub add http
import 'package:http/http.dart' as http;
android/app/src/main/AndroidManifest.xml within the <manifest> tag:
<uses-permission android:name="android.permission.INTERNET" />
macos/Runner/DebugProfile.entitlements and macos/Runner/Release.entitlements within the <dict> tag:
<key>com.apple.security.network.client</key>
<true/>
Design robust REST clients by applying these best practices:
Uri.parse('url'). Never pass raw strings to client calls.HttpHeaders.authorizationHeader key from dart:io.jsonEncode from dart:convert.200 OK or 201 Created).null on failure, as this hides issues and results in infinite UI loading spinners.http.Client dependency in your network classes instead of calling standard global methods. This facilitates easy testing and mock injection.Offload JSON decoding and mapping to a background thread to prevent UI jank (dropped frames) when handling payloads larger than 1MB:
package:flutter/foundation.dart.compute() function to spawn a background isolate.Follow this checklist to build and verify network integration:
fromJson factory constructor.http.Client.'Content-Type': 'application/json; charset=UTF-8' and attach jsonEncode data.200 OK.FutureBuilder or state management controller in the UI layer.This example demonstrates setting up a robust, testable network client that parses complex payload lists in a background isolate.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
// 1. Top-level parsing function (required to run in a separate Isolate)
List<Product> parseProducts(String responseBody) {
final parsed = (jsonDecode(responseBody) as List<dynamic>).cast<Map<String, dynamic>>();
return parsed.map<Product>((json) => Product.fromJson(json)).toList();
}
// 2. Service Layer exposing testable methods
class ProductService {
final http.Client client;
const ProductService({required this.client});
Future<List<Product>> fetchProducts() async {
final response = await client.get(
Uri.parse('https://api.example.com/products'),
headers: {
HttpHeaders.acceptHeader: 'application/json',
HttpHeaders.authorizationHeader: 'Bearer token_here',
},
);
if (response.statusCode == 200) {
// Offload heavy JSON parsing of larger lists to a background isolate
return compute(parseProducts, response.body);
} else {
throw HttpException('Failed to load products. Status: ${response.statusCode}');
}
}
}
// 3. Immutable Data Model
class Product {
final int id;
final String name;
final double price;
const Product({
required this.id,
required this.name,
required this.price,
});
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'] as int,
name: json['name'] as String,
price: (json['price'] as num).toDouble(),
);
}
}
// 4. UI Layer Integration
class ProductListView extends StatefulWidget {
final ProductService productService;
const ProductListView({
super.key,
required this.productService,
});
@override
State<ProductListView> createState() => _ProductListViewState();
}
class _ProductListViewState extends State<ProductListView> {
late Future<List<Product>> _futureProducts;
@override
void initState() {
super.initState();
// Cache the future once to prevent redundant re-fetching on rebuilds
_futureProducts = widget.productService.fetchProducts();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Store Products'),
),
body: FutureBuilder<List<Product>>(
future: _futureProducts,
builder: (context, snapshot) {
if (snapshot.hasData) {
final products = snapshot.data!;
return ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
final product = products[index];
return ListTile(
title: Text(product.name),
trailing: Text('\$${product.price.toStringAsFixed(2)}'),
);
},
);
} else if (snapshot.hasError) {
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
'An error occurred: ${snapshot.error}',
textAlign: TextAlign.center,
),
),
);
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
development
Configure internationalization and localization support using Flutter's built-in l10n system, App Resource Bundle (ARB) files, and ICU formatting syntax.
development
Create model classes with fromJson/toJson using dart:convert and Dart 3 pattern matching. Use when manually mapping JSON to classes, parsing HTTP responses, or choosing between manual and code-generated serialization.
data-ai
Diagnose and fix Flutter layout constraint violations (RenderFlex overflow, unbounded height/width, ParentData misuse). Use when encountering layout exceptions, yellow-black overflow stripes, or red error screens.
development
Build adaptive layouts using LayoutBuilder, MediaQuery, or Expanded/Flexible widgets to ensure the UI looks elegant across all mobile, tablet, and desktop form factors.