flutter-networking by madteacher/mad-agents-skills
npx skills add https://github.com/madteacher/mad-agents-skills --skill flutter-networking在 pubspec.yaml 中添加 HTTP 依赖:
dependencies:
http: ^1.6.0
基本的 GET 请求:
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<Album> fetchAlbum() async {
final response = await http.get(
Uri.parse('https://api.example.com/albums/1'),
);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
在 UI 中使用 FutureBuilder:
FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.title);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
)
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
用于检索数据。完整示例请参阅 http-basics.md。
用于创建新资源。需要 Content-Type: application/json 请求头。
final response = await http.post(
Uri.parse('https://api.example.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'title': title}),
);
POST 示例请参阅 http-basics.md。
用于更新现有资源。
final response = await http.put(
Uri.parse('https://api.example.com/albums/1'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'title': title}),
);
用于删除资源。
final response = await http.delete(
Uri.parse('https://api.example.com/albums/1'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
添加 WebSocket 依赖:
dependencies:
web_socket_channel: ^3.0.3
基本的 WebSocket 连接:
import 'package:web_socket_channel/web_socket_channel.dart';
final _channel = WebSocketChannel.connect(
Uri.parse('wss://echo.websocket.events'),
);
// 监听消息
StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : '');
},
)
// 发送消息
_channel.sink.add('Hello');
// 关闭连接
_channel.sink.close();
完整的 WebSocket 实现请参阅 websockets.md。
在请求中添加授权请求头:
import 'dart:io';
final response = await http.get(
Uri.parse('https://api.example.com/data'),
headers: {HttpHeaders.authorizationHeader: 'Bearer $token'},
);
常见的身份验证模式:
Authorization: Bearer <token>Authorization: Basic <base64_credentials>X-API-Key: <key>详细的身份验证策略请参阅 authentication.md。
正确处理 HTTP 错误:
if (response.statusCode >= 200 && response.statusCode < 300) {
return Data.fromJson(jsonDecode(response.body));
} else if (response.statusCode == 401) {
throw UnauthorizedException();
} else if (response.statusCode == 404) {
throw NotFoundException();
} else {
throw ServerException();
}
全面的错误处理策略请参阅 error-handling.md。
对于大型 JSON 响应,使用 compute() 在后台 isolate 中解析:
import 'package:flutter/foundation.dart';
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response = await client.get(
Uri.parse('https://api.example.com/photos'),
);
return compute(parsePhotos, response.body);
}
List<Photo> parsePhotos(String responseBody) {
final parsed = (jsonDecode(responseBody) as List)
.cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
优化技术请参阅 performance.md。
当使用 MVVM 架构时(请参阅 flutter-architecture):
服务示例:
class AlbumService {
final http.Client _client;
AlbumService(this._client);
Future<Album> fetchAlbum(int id) async {
final response = await _client.get(
Uri.parse('https://api.example.com/albums/$id'),
);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
}
数据类型的单一数据源:
class AlbumRepository {
final AlbumService _service;
final LocalStorage _cache;
Future<Album> getAlbum(int id) async {
try {
return await _cache.getAlbum(id) ??
await _service.fetchAlbum(id);
} catch (e) {
throw AlbumFetchException();
}
}
}
为失败的请求实现指数退避:
Future<T> fetchWithRetry<T>(
Future<T> Function() fetch, {
int maxRetries = 3,
}) async {
for (int i = 0; i < maxRetries; i++) {
try {
return await fetch();
} catch (e) {
if (i == maxRetries - 1) rethrow;
await Future.delayed(Duration(seconds: 2 << i));
}
}
throw StateError('Unreachable');
}
fromJson 工厂方法的类型安全模型类fetch_example.dart - 使用 FutureBuilder 的完整 GET 请求post_example.dart - POST 请求实现websocket_example.dart - 带有流处理的 WebSocket 客户端auth_example.dart - 身份验证请求示例background_parsing.dart - 使用 compute() 进行 JSON 解析http_service.dart - 可复用的 HTTP 服务模板repository_template.dart - 仓库模式模板每周安装量
424
仓库
GitHub Stars
87
首次出现
Jan 22, 2026
安全审计
安装于
codex323
opencode285
gemini-cli274
github-copilot256
amp235
kimi-cli234
Add HTTP dependency to pubspec.yaml:
dependencies:
http: ^1.6.0
Basic GET request:
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<Album> fetchAlbum() async {
final response = await http.get(
Uri.parse('https://api.example.com/albums/1'),
);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
Use in UI with FutureBuilder:
FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.title);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
)
Use for retrieving data. See http-basics.md for complete examples.
Use for creating new resources. Requires Content-Type: application/json header.
final response = await http.post(
Uri.parse('https://api.example.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'title': title}),
);
See http-basics.md for POST examples.
Use for updating existing resources.
final response = await http.put(
Uri.parse('https://api.example.com/albums/1'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{'title': title}),
);
Use for deleting resources.
final response = await http.delete(
Uri.parse('https://api.example.com/albums/1'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
Add WebSocket dependency:
dependencies:
web_socket_channel: ^3.0.3
Basic WebSocket connection:
import 'package:web_socket_channel/web_socket_channel.dart';
final _channel = WebSocketChannel.connect(
Uri.parse('wss://echo.websocket.events'),
);
// Listen for messages
StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : '');
},
)
// Send message
_channel.sink.add('Hello');
// Close connection
_channel.sink.close();
See websockets.md for complete WebSocket implementation.
Add authorization headers to requests:
import 'dart:io';
final response = await http.get(
Uri.parse('https://api.example.com/data'),
headers: {HttpHeaders.authorizationHeader: 'Bearer $token'},
);
Common authentication patterns:
Authorization: Bearer <token>Authorization: Basic <base64_credentials>X-API-Key: <key>See authentication.md for detailed authentication strategies.
Handle HTTP errors appropriately:
if (response.statusCode >= 200 && response.statusCode < 300) {
return Data.fromJson(jsonDecode(response.body));
} else if (response.statusCode == 401) {
throw UnauthorizedException();
} else if (response.statusCode == 404) {
throw NotFoundException();
} else {
throw ServerException();
}
See error-handling.md for comprehensive error handling strategies.
For large JSON responses, use compute() to parse in background isolate:
import 'package:flutter/foundation.dart';
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response = await client.get(
Uri.parse('https://api.example.com/photos'),
);
return compute(parsePhotos, response.body);
}
List<Photo> parsePhotos(String responseBody) {
final parsed = (jsonDecode(responseBody) as List)
.cast<Map<String, dynamic>>();
return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
}
See performance.md for optimization techniques.
When using MVVM architecture (see flutter-architecture):
Example service:
class AlbumService {
final http.Client _client;
AlbumService(this._client);
Future<Album> fetchAlbum(int id) async {
final response = await _client.get(
Uri.parse('https://api.example.com/albums/$id'),
);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
}
Single source of truth for data type:
class AlbumRepository {
final AlbumService _service;
final LocalStorage _cache;
Future<Album> getAlbum(int id) async {
try {
return await _cache.getAlbum(id) ??
await _service.fetchAlbum(id);
} catch (e) {
throw AlbumFetchException();
}
}
}
Implement exponential backoff for failed requests:
Future<T> fetchWithRetry<T>(
Future<T> Function() fetch, {
int maxRetries = 3,
}) async {
for (int i = 0; i < maxRetries; i++) {
try {
return await fetch();
} catch (e) {
if (i == maxRetries - 1) rethrow;
await Future.delayed(Duration(seconds: 2 << i));
}
}
throw StateError('Unreachable');
}
fromJson factoriesfetch_example.dart - Complete GET request with FutureBuilderpost_example.dart - POST request implementationwebsocket_example.dart - WebSocket client with stream handlingauth_example.dart - Authenticated request examplebackground_parsing.dart - compute() for JSON parsinghttp_service.dart - Reusable HTTP service templaterepository_template.dart - Repository pattern templateWeekly Installs
424
Repository
GitHub Stars
87
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
codex323
opencode285
gemini-cli274
github-copilot256
amp235
kimi-cli234
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
136,300 周安装
SaaS财务指标速查表:收入、CAC、LTV等关键公式与基准快速查阅
414 周安装
iOS WidgetKit 开发指南:构建主屏幕、锁屏、灵动岛小组件与实时活动
414 周安装
web-haptics:为Web应用添加触觉反馈的JavaScript库,支持React、Vue、Svelte框架
414 周安装
PyTorch深度学习开发专家 | Transformer、扩散模型、LLM开发指南与最佳实践
414 周安装
OMX:OpenAI Codex CLI 多智能体编排工具,实现AI驱动开发工作流自动化
414 周安装
MAESTRO:AI驱动的精英软件架构治理框架 - 提升代码质量与项目连续性
414 周安装