草庐IT

flutter - 已经听过流,换屏后报错

coder 2023-07-22 原文

我在我的应用程序中应用了 Bloc 模式,但在通过底部导航更改屏幕时遇到了问题。

我尝试进行流式广播,但是在更改屏幕后数据消失了。

这是我的 Home 类,它代表主屏幕。

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final DepartmentBloc departmentBloc = BlocProvider.getBloc<DepartmentBloc>();
  final PromotionProductBloc promotionProductBloc = BlocProvider.getBloc<PromotionProductBloc>();

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      physics: BouncingScrollPhysics(),
      child: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            CustomText(
              text: "Promoções",
              fontSize: 20.0,
              padding: EdgeInsets.only(left: 12.0),
            ),
            StreamBuilder(
              stream: promotionProductBloc.outPromotionProducts,
              builder: (context, snapshot) {
                if (snapshot.hasData)
                  return Container(
                    height: 230.0,
                    child: ListView.builder(
                      padding: EdgeInsets.only(
                          left: 12.0, right: 12.0, bottom: 10.0),
                      itemBuilder: (context, index) {
                        return PromotionProductCard(snapshot.data[index]);
                      },
                      itemCount: snapshot.data.length,
                      scrollDirection: Axis.horizontal,
                      physics: BouncingScrollPhysics(),
                    ),
                  );
                else
                  return Container(
                    height: 200.0,
                  );
              },
            ),
            CustomText(
              text: "Categorias",
              fontSize: 20.0,
              padding: EdgeInsets.only(left: 12.0, top: 20.0, bottom: 10.0),
            ),
            StreamBuilder(
              stream: departmentBloc.outDepartments,
              builder: (context, snapshot) {
                if (snapshot.hasData)
                  return Container(
                    height: 120.0,
                    child: ListView.builder(
                      padding: EdgeInsets.only(left: 12.0, right: 12.0),
                      itemBuilder: (context, index) {
                        return CategoryCard(snapshot.data[index]);
                      },
                      itemCount: snapshot.data.length,
                      scrollDirection: Axis.horizontal,
                      physics: BouncingScrollPhysics(),
                    ),
                  );
                else
                  return Container(
                    height: 100.0,
                  );
              },
            ),
          ],
        ),
      ),
    );
  }

这是访问 Service 以获取数据并打开 Stream 导出的类。

class DepartmentBloc extends BlocBase {

  DepartmentService departmentService;

  List<Department> departments;

  final StreamController<List<Department>> _departmentController = StreamController<List<Department>>();
  Stream<List<Department>> get outDepartments => _departmentController.stream;

  DepartmentBloc() {
    departmentService = DepartmentService();
    getAll();
  }

  void getAll() async {
    departments =  await departmentService.getAll();
    _departmentController.sink.add(departments);
  }

  @override
  void dispose() {
    _departmentController.close();
    super.dispose();
  }
}

PromotionProductBloc 与 DepartmentBloc 相同,它只是更改服务访问权限。

我正在使用底部导航,在转到另一个屏幕并返回后出现以下错误:流已被收听。

最佳答案

尝试

  final StreamController<List<Department>> _departmentController = StreamController.broadcast<List<Department>>();

似乎您正在使用单个订阅流 Controller 。他们只能听一次。它们的一个优点是第一个监听器将接收在订阅它之前发出的那些事件。广播流不保证这种行为,但它们可以被多次订阅。

关于flutter - 已经听过流,换屏后报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56606184/

有关flutter - 已经听过流,换屏后报错的更多相关文章

  1. 深度学习部署:Windows安装pycocotools报错解决方法 - 2

    深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal

  2. ruby - 确定 ruby​​ 脚本是否已经在运行 - 2

    有没有一种简单的方法可以判断ruby​​脚本是否已经在运行,然后适本地处理它?例如:我有一个名为really_long_script.rb的脚本。我让它每5分钟运行一次。当它运行时,我想看看之前运行的是否还在运行,然后停止第二个脚本的执行。有什么想法吗? 最佳答案 ps是一种非常糟糕的方法,并且可能会出现竞争条件。传统的Unix/Linux方法是将PID写入文件(通常在/var/run中)并在启动时检查该文件是否存在。例如pid文件位于/var/run/myscript.pid然后你会在运行程序之前检查它是否存在。有一些技巧可以避免

  3. ruby-on-rails - phusion passenger 和 ruby​​ 1.9.1 已经开始工作了吗? - 2

    我有一台生产机器和一台开发机器,都运行ubuntu8.10并且都运行最新的phusionpassenger。当我在osx上的本地开发机器上使用ruby​​1.9.1时,我想知道外面的人是否已经在使用带有ruby​​1.9.1甚至1.9.2的phusionpassenger?如果是这样,请告诉我们您的设置!此外,有没有办法在apache上使用phusionpassenger同时运行ruby​​1.8.7(ree)和1.9.1?感谢您的指点,我在任何地方都找不到任何提示... 最佳答案 是的,从某些2.2.x版本开始就正式支持它,我不记

  4. ruby - 当我已经在使用 %r 时,为什么 rubocop 要求我放置//围绕正则表达式? - 2

    我有以下正则表达式regexp=%r{((returned|undelivered)\smail|mail\sdelivery(\sfailed)?)}x但是当我在上面运行rubocop时,它会提示我需要“在正则表达式周围使用//”。我怎样才能绕过它? 最佳答案 您可以通过将.rubocop.yml文件添加到项目文件夹的根目录并设置适当的配置来禁用(和启用)任何rubocopcop。要查看您可以做什么,请查看rubocop包中的全局default.yml。它有完整的评论。对于这个特殊问题,创建一个.rubocop.yml和...要完

  5. ruby-on-rails - 报错 - 在 Snow Leopard 上安装 RVM - 2

    我正在尝试在我的SnowLeopard10.6.8上安装RVM,方法是:\curl-Lhttps://get.rvm.io|bash-sstable--ruby我得到这个错误:InstallingRubyfromsourceto:/Users/Villa/.rvm/rubies/ruby-2.0.0-p0,thismaytakeawhiledependingonyourcpu(s)...ruby-2.0.0-p0-#downloadingruby-2.0.0-p0,thismaytakeawhiledependingonyourconnection...ruby-2.0.0-p0-#e

  6. Unity 报错No ‘git‘ executable was found. Please install Git on your system then restart - 2

    亲测可用。Anerroroccurredwhileresolvingpackages:Projecthasinvaliddependencies: com.unity.xxx:No'git'executablewasfound.PleaseinstallGitonyour  systemthenrestartUnityandUnityHub在我们使用PackageManager时,Unity允许我们使用Git上的package(点击加号,选择addpackagefromgitURL,或者是直接在Asset/Packages/manifest.json中添加包名)。但是这种操作需要我们事先装好g

  7. Flutter 环境变量配置和flutter doctor中的错误解决 - 2

    一、环境变量右键点击我的电脑-属性:然后找到环境变量 1.Android的SDK不在C盘的话需要额外配这个到用户环境变量:ANDROID_HOMED:\AndroidSDK2.然后在系统变量:Path中添加一条这样的值        D:\Flutter\flutter\bin             这个值写flutter包解压的实际地址即可 3.在系统变量中添加两个镜像变量:        变量名:FLUTTER_STORAGE_BASE_URL      变量值:https://storage.flutter-io.cn        变量名:PUB_HOSTED_URL      变量

  8. ruby-on-rails - 您已经激活了 spring 1.3.6,但是您的 Gemfile 需要 spring 1.3.3。 ( gem ::加载错误) - 2

    我今天遇到了同样的问题,有一个建议:在您的命令前添加bundleexec可能会解决此问题。前置bundleexec没有帮助(我已经这样做了)。springstop和springrestart没有帮助。我需要做的:bundleupdatespring这对我有用。在之前的gemlock文件中使用spring版本是否有更好的解决方案? 最佳答案 我删除gemfile.lock并运行bundle通常会清除一切。否则只需从Gemfile中删除gem"spring"并运行bundle 关于ruby-

  9. ruby-on-rails - Rails/Ruby创建数据库报错: Unable to load the EventMachine C extension - 2

    更新:eventmachinegem已安装并在我的gemfile中:eventmachine(1.0.0,0.12.10)请帮忙!尝试使用以下内容创建数据库:Fitzs-MacBook-Pro:twilio_insanityFitz$rakedb:create'返回以下错误:UnabletoloadtheEventMachineCextension;Tousethepure-rubyreactor,require'em/pure_ruby'rakeaborted!cannotloadsuchfile--rubyeventmachine/Users/Fitz/.rvm/gems/ruby

  10. nginx配置https后报错nginx: [emerg] https protocol requires SSL support in XXX.conf详细解决方法 - 2

    一、前言最近,在测试环境的nginx里增加了一个https配置:location/api-meeting-qq/{proxy_passhttps://api.meeting.qq.com/;}然后,执行命令://这个是nginx启动文件的路径,根据实际情况自行更改sudo/home/useradmin/nginx/sbin/nginx-sreload结果,nginx就报错了:nginx:[emerg]httpsprotocolrequiresSSLsupportin/home/useradmin/nginx/conf.d/trainNginx.conf:9二、解决方法百度发现,是之前安装ngi

随机推荐