代码报错import dartconvert;import packagefluttermaterialdart;import packagediodiodart;class StreamTextPage extends StatefulWidget override _StreamTextPageState createState = _StreamTextPageState;class
将 _getTextStream() 方法的返回值改为 Stream<Uint8List>。修改后的代码如下:
Stream<Uint8List> _getTextStream() async* {
final dio = Dio();
final response = await dio.get(
'http://127.0.0.1:9000/?content=%E7%BB%99%E6%88%91%E5%86%99%E4%B8%80%E7%AF%87300%E5%AD%97%E7%9A%84%E6%96%87%E7%AB%A0%EF%BC%8C%E5%85%B3%E4%BA%8E%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD',
options: Options(responseType: ResponseType.stream));
yield* response.data.stream();
}
同时,需要将 utf8.decode(data) 改为 String.fromCharCodes(data),如下:
_getTextStream().listen((data) {
setState(() {
_text = String.fromCharCodes(data);
});
});
``
原文地址: https://www.cveoy.top/t/topic/d1lm 著作权归作者所有。请勿转载和采集!