Loading...
Loading...
Executes HTTP requests and handles JSON serialization in a Flutter app. Use when integrating with REST APIs or parsing structured data from external sources.
npx skill4agent add flutter/skills flutter-handling-http-and-jsonnetwork_security_config.xmlNSAppTransportSecurityUri.https(authority, unencodedPath, [queryParameters])http.Response.statusCode200201nullcompute()application/jsonhttphttppubspec.yamlAndroidManifest.xml.entitlementsUrihttp.get(uri)http.post(uri, headers: {...}, body: jsonEncode(data))Content-Typeapplication/json; charset=UTF-8http.put(uri, headers: {...}, body: jsonEncode(data))http.delete(uri, headers: {...})response.statusCode200201jsonDecode(response.body)Exception('Failed to load/update/delete resource')dart:convertjson_serializabledart:convertfinalfactory Model.fromJson(Map<String, dynamic> json)Map<String, dynamic> toJson()json_serializableflutter pub add json_annotationflutter pub add -d build_runner json_serializablejson_annotation.dartpart 'model_name.g.dart';@JsonSerializable()explicitToJson: truefromJsontoJsondart run build_runner build --delete-conflicting-outputsStringList<Model>jsonDecoderesponse.bodycompute()import 'dart:convert';
import 'package:http/http.dart' as http;
class Album {
final int id;
final String title;
const Album({required this.id, required this.title});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
id: json['id'] as int,
title: json['title'] as String,
);
}
}
Future<Album> fetchAlbum() async {
final uri = Uri.https('jsonplaceholder.typicode.com', '/albums/1');
final response = await http.get(uri);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
} else {
throw Exception('Failed to load album');
}
}Future<Album> createAlbum(String title) async {
final uri = Uri.https('jsonplaceholder.typicode.com', '/albums');
final response = await http.post(
uri,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'title': title}),
);
if (response.statusCode == 201) {
return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
} else {
throw Exception('Failed to create album.');
}
}computeimport 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
// 1. Top-level function for parsing
List<Photo> parsePhotos(String responseBody) {
final parsed = (jsonDecode(responseBody) as List<Object?>)
.cast<Map<String, Object?>>();
return parsed.map<Photo>(Photo.fromJson).toList();
}
// 2. Fetch function using compute
Future<List<Photo>> fetchPhotos(http.Client client) async {
final uri = Uri.https('jsonplaceholder.typicode.com', '/photos');
final response = await client.get(uri);
if (response.statusCode == 200) {
// Run parsePhotos in a separate isolate
return compute(parsePhotos, response.body);
} else {
throw Exception('Failed to load photos');
}
}json_serializableimport 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
(explicitToJson: true)
class User {
final String name;
(name: 'registration_date_millis')
final int registrationDateMillis;
User(this.name, this.registrationDateMillis);
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}