【问题标题】:Flutter : StreamSink<dynamic> can't be returned from the function 'chuckDataSink' because it has a return type of StreamSink<Response<chuckResponse>>?Flutter:无法从函数“chuckDataSink”返回 StreamSink<dynamic>,因为它的返回类型为 StreamSink<Response<chuckResponse>>?
【发布时间】:2021-07-11 17:39:03
【问题描述】:

通过迁移到 Flutter 2,我收到以下错误。

lib/bloc/chuck_category_bloc.dart:12:7: 错误: 'StreamSink?' 类型的值无法从返回类型为“StreamSink?”的函数返回。

  • “StreamSink”来自“dart:async”。
  • “响应”来自“package:upgrade_noyelling/networking/Response.dart”(“lib/networking/Response.dart”)。
  • “chuckCategories”来自“package:upgrade_noyelling/models/chuck_categories.dart”(“lib/models/chuck_categories.dart”)。 _chuckListController?.sink; ^ lib/bloc/chuck_category_bloc.dart:15:7: 错误:“流”类型的值?无法从返回类型为“Stream?”的函数返回。
  • “流”来自“dart:async”。
  • “响应”来自“package:upgrade_noyelling/networking/Response.dart”(“lib/networking/Response.dart”)。
  • “chuckCategories”来自“package:upgrade_noyelling/models/chuck_categories.dart”(“lib/models/chuck_categories.dart”)。 _chuckListController?.stream; ^

下面是代码

import 'dart:async';
import 'package:upgrade_noyelling/models/chuck_response.dart';
import 'package:upgrade_noyelling/networking/Response.dart';
import 'package:upgrade_noyelling/repository/chuck_repository.dart';

class ChuckBloc {
   ChuckRepository ? _chuckRepository;
   StreamController ?  _chuckDataController;
   bool ? _isStreaming;

  StreamSink<Response<chuckResponse>>? get chuckDataSink =>
      _chuckDataController.sink;

  Stream<Response<chuckResponse>> get chuckDataStream =>
      _chuckDataController!.stream;

  ChuckBloc(String category) {
    _chuckDataController = StreamController<Response<chuckResponse>>();
    _chuckRepository = ChuckRepository();
    _isStreaming = true;
    fetchChuckyJoke(category);
  }

  fetchChuckyJoke(String category) async {
    chuckDataSink!.add(Response.loading('Getting a Chucky joke!'));
    try {
      chuckResponse chuckJoke = await _chuckRepository!.fetchChuckJoke(category);
      if (_isStreaming!) chuckDataSink!.add(Response.completed(chuckJoke));
    } catch (e) {
      if (_isStreaming!) chuckDataSink!.add(Response.error(e.toString()));
      print(e);
    }
  }

  dispose() {
    _isStreaming = false;
    _chuckDataController?.close();
  }
}

【问题讨论】:

标签: flutter stream-builder


【解决方案1】:

升级到 Flutter 2 后,我遇到了与您完全相同的问题。我设法通过以下方法解决了这个问题:

    class ChuckBloc {
       ChuckRepository ? _chuckRepository;
       final  _chuckDataController = StreamController<Response<chuckResponse>>();
       bool ? _isStreaming;

       StreamSink<Response<chuckResponse>> get chuckDataSink =>_chuckDataController.sink;

       Stream<Response<chuckResponse>> get chuckDataStream =>_chuckDataController!.stream;

       ChuckBloc(String category) {
          _chuckRepository = ChuckRepository();
          _isStreaming = true;
          fetchChuckyJoke(category);
       }
       
       //the rest of your code here...
       
    }

您可以删除“?”在chuckDataSink 上也是如此,因为它不会为空

【讨论】:

    【解决方案2】:

    我可以通过在项目的 pubspec.yaml 文件中设置 SDK 约束来关闭空安全性来解决此问题。早些时候,我的 sdk 是“2.12.0

    https://we-launch.com/turn-off-null-safety-in-flutter/

    【讨论】:

      【解决方案3】:

      dart 2.12 是一些带有空值的提取物

      所以

      _chuckDataController 从一开始就没有被初始化

      ChuckBloc {
      final _chuckDataController PublishSubject<Response<chuckResponse>>();
      StreamSink<Response<chuckResponse>> get chuckDataSink =>   _chuckDataController.sink;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        • 2022-01-26
        • 2022-10-12
        • 2021-01-25
        • 2021-08-20
        • 1970-01-01
        • 2021-10-02
        相关资源
        最近更新 更多