Download code/flutter_integration_example.dart from luom2645/agent-workspace: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/luom2645/agent-workspace/resolve/7d4c5ffdb4518a5d7456ef06ab77458fcf42663a/code/flutter_integration_example.dart
- Command line
-
hf download hf://luom2645/agent-workspace@7d4c5ffdb4518a5d7456ef06ab77458fcf42663a/code/flutter_integration_example.dart
-
curl -L -o flutter_integration_example.dart https://huggingface.co/luom2645/agent-workspace/resolve/7d4c5ffdb4518a5d7456ef06ab77458fcf42663a/code/flutter_integration_example.dart
2.93 kB
| ```dart | |
| import 'package:dio/dio.dart'; | |
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
| import 'dart:convert'; | |
| // 1. 定义传递给后端代理的参数对象 | |
| class GenerateTextParams { | |
| final String prompt; | |
| final String model; // e.g., 'claude-3.5-sonnet', 'gpt-4o' | |
| final double temperature; | |
| // ... 其他用户偏好,如写作风格、角色设定等 | |
| GenerateTextParams({ | |
| required this.prompt, | |
| required this.model, | |
| this.temperature = 0.7, | |
| }); | |
| Map<String, dynamic> toJson() => { | |
| 'prompt': prompt, | |
| 'model': model, | |
| 'temperature': temperature, | |
| }; | |
| } | |
| // 2. 使用Riverpod创建RepositoryProvider | |
| // 使用Dio进行网络请求 | |
| final aiRepositoryProvider = Provider<AIRepository>((ref) { | |
| return AIRepository(Dio()); | |
| }); | |
| // 3. 创建AI Repository来封装API调用逻辑 | |
| class AIRepository { | |
| final Dio _dio; | |
| // 重要的安全提示:此URL应指向您自己的后端代理,而不是直接指向AI提供商! | |
| final String _proxyUrl = 'https://your-backend-proxy.com/generate'; | |
| AIRepository(this._dio); | |
| // 返回一个字符串流,用于实时显示生成的文本 | |
| Stream<String> generateTextStream(GenerateTextParams params) { | |
| try { | |
| // 发起POST请求到后端代理,请求体中包含所需参数 | |
| // 设置响应类型为stream,以便处理流式数据 | |
| return _dio.post( | |
| _proxyUrl, | |
| data: params.toJson(), | |
| options: Options(responseType: ResponseType.stream), | |
| ).asStream().asyncExpand((response) { | |
| // 将收到的字节流(Stream<List<int>>)转换为UTF8字符串流(Stream<String>) | |
| return response.data.stream.transform(utf8.decoder); | |
| }); | |
| } on DioError catch (e) { | |
| // 处理网络错误或API错误 | |
| print('Error calling AI proxy: $e'); | |
| return Stream.error('Failed to generate text: ${e.message}'); | |
| } | |
| } | |
| } | |
| // 4. 使用StreamProvider.family来根据参数获取并监听文本生成流 | |
| // .family修饰符允许我们向provider传递参数(即GenerateTextParams) | |
| final aiGeneratedTextProvider = | |
| StreamProvider.family<String, GenerateTextParams>((ref, params) { | |
| // 监听repository provider | |
| final repository = ref.watch(aiRepositoryProvider); | |
| // 调用repository的方法并返回流 | |
| return repository.generateTextStream(params); | |
| }); | |
| /* | |
| * --- 如何在Flutter UI中使用 --- | |
| * | |
| * class WritingScreen extends ConsumerWidget { | |
| * @override | |
| * Widget build(BuildContext context, WidgetRef ref) { | |
| * final params = GenerateTextParams(prompt: '从前有座山...', model: 'claude-3.5-sonnet'); | |
| * final aiStream = ref.watch(aiGeneratedTextProvider(params)); | |
| * | |
| * return aiStream.when( | |
| * loading: () => CircularProgressIndicator(), | |
| * error: (err, stack) => Text('Error: $err'), | |
| * data: (text) => Text(text), // 这将随着新文本的到来而实时更新 | |
| * ); | |
| * } | |
| * } | |
| * | |
| */ | |
| ``` |