帮我把下面代码改一下我需要在接收数据完成后关闭定时器并清空xdata try final response = await dioget httpschatgpt-chatgpt-lswirmtbkxus-east-1fcappruncontent= + Globalmessages_pure; if responsedat
try { Response response = await Dio().post( 'http://127.0.0.1:5000/test', options: Options( responseType: ResponseType.stream, ), ); int current = 1; bool end = false; Timer timer; timer = Timer.periodic(Duration(seconds: 1), (timer) { if (end) { timer.cancel(); } if (current == 0) { current = 1; setState(() {}); } else { current--; print(current); } }); final textMessage = types.TextMessage( author: types.User(id: '131231-322-4a89-ae75-a22bf8d6f3ac'), createdAt: DateTime.now().millisecondsSinceEpoch, id: const Uuid().v4(), text: xdata, ); response.data.stream.transform(StreamTransformer<Uint8List, String>.fromHandlers( handleData: (data, sink) { if (data.isNotEmpty) { sink.add(utf8.decode(data)); } }, )).listen((data) { xdata += data; print(data); if (data.isEmpty) { end = true; xdata = ''; timer.cancel(); } Global.messages[0] = types.TextMessage( author: types.User(id: '131231-322-4a89-ae75-a22bf8d6f3ac'), createdAt: DateTime.now().millisecondsSinceEpoch, id: const Uuid().v4(), text: xdata, ); }); Global.messages_pure += " AI: '" + response.data.toString() + "';"; } on DioError catch (e) { if (e.type == DioErrorType.connectTimeout || e.type == DioErrorType.receiveTimeout) { } else { print(e); }
原文地址: https://www.cveoy.top/t/topic/ffKS 著作权归作者所有。请勿转载和采集!