Flutter Stream Text: 解决'NoSuchMethodError: Class '_BoundSinkStream...' has no instance method 'call''错误

在使用 Flutter 开发应用时,我们经常需要从网络获取数据并进行展示。当使用 Stream 获取文本数据时,可能会遇到如下错误:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: Class '_BoundSinkStream<List<int>, Uint8List>' has no
instance method 'call'.
[        ] Receiver: Instance of '_BoundSinkStream<List<int>, Uint8List>'
[        ] Tried calling: call()
[        ] #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)        
[        ] #1      _StreamTextPageState._getTextStream (package:muse_nepu_course/chat_stream.dart:30:26)
[        ] <asynchronous suspension>
[        ] #2      _StreamTextPageState.initState.<anonymous closure> (package:muse_nepu_course/chat_stream.dart:17:29)

这个错误通常出现在使用 Dio 库获取网络数据并将其解析为文本字符串时。错误原因是,Dio 库返回的 StreamStream<List<int>> 类型,而 utf8.decode() 函数需要的是 List<int> 类型的参数。

解决方案

1. 修改 _getTextStream() 方法的返回值类型

_getTextStream() 方法的返回值类型改为 Stream<Uint8List>,并使用 response.data.stream() 获取 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();
}

2. 使用 String.fromCharCodes() 解析数据

utf8.decode(data) 改为 String.fromCharCodes(data),将 Uint8List 解析为字符串。

_getTextStream().listen((data) {
  setState(() {
    _text = String.fromCharCodes(data);
  });
});

完整代码

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class StreamTextPage extends StatefulWidget {
  @override
  _StreamTextPageState createState() => _StreamTextPageState();
}

class _StreamTextPageState extends State<StreamTextPage> {
  String _text = '';

  @override
  void initState() {
    super.initState();

    _getTextStream().listen((data) {
      setState(() {
        _text = String.fromCharCodes(data);
      });
    });
  }

  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();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Stream Text'),
      ),
      body: Center(
        child: Text(_text),
      ),
    );
  }
}

经过以上修改,代码将能够正常运行,并从网络获取文本数据并进行展示。

注意:

  • String.fromCharCodes(data) 方法可以将 Uint8List 解析为字符串。
  • response.data.stream() 方法返回的是 Stream<Uint8List> 类型。
  • 以上代码仅供参考,实际应用中可能需要根据具体的项目需求进行调整。
Flutter Stream Text: 解决'NoSuchMethodError: Class '_BoundSinkStream...' has no instance method 'call''错误

原文地址: https://www.cveoy.top/t/topic/nMtn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录