Code Monkey home page Code Monkey logo

Comments (3)

smurat avatar smurat commented on September 23, 2024

I'm having a similar problem

from riverpod.

rrousselGit avatar rrousselGit commented on September 23, 2024

Theres nothing wrong here. Your provider wasn't disposed. So the Stream is still active.
Use autoDispose.

from riverpod.

tobiiasl avatar tobiiasl commented on September 23, 2024

Perhaps the test setup is just confusing. I will instead provide code closer to the real use case below.

Our app has a provider which exposes a stream from an event channel, basically just return eventChannel.receiveBroadcastStream(). On the Java side, we start using HW resources when the event channel is listened to (in the Java onListen() method) and stop using them when the event channel no longer has any listeners (in the Java onCancel() method). The problem is that the event channel stream never gets unsubscribed from and therefore the Java onCancel() method never gets executed.

In the code below I have replaced the EventChannel object with a simple StreamController().stream object to keep things simple and in a single file. But think of it as an event channel.

When toggling between the two views I expect to see both Event: onListen and Event: onCancel being printed. But only Event: onListen gets printed once.

The goal is to make the onCancel() method on the Java side being called when no widget is listening to the event channel stream. How can that be achieved?

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// This is simplified code (to not have to implement Java code).
final eventChannelStreamController = StreamController<int>();
final eventChannelStream = eventChannelStreamController.stream;

// More similar to real code:
// const eventChannel = EventChannel('event_channel');
// final eventChannelStream = eventChannel.receiveBroadcastStream();

final streamProvider = StreamProvider.autoDispose<int>((ref) {
  ref.onDispose(() {
    // Should something be done here?
  });

  return eventChannelStream.asBroadcastStream();
});

void main() {
  eventChannelStreamController.onListen = () {
    print('Event: onListen');
  };

  eventChannelStreamController.onCancel = () {
    print('Event: onCancel');
  };

  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  bool showTestWidget = true;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body:
            showTestWidget ? const TestWidget() : const Text('Another widget'),
        floatingActionButton: FloatingActionButton(
          onPressed: () => setState(() => showTestWidget = !showTestWidget),
          child: Icon(showTestWidget ? Icons.visibility_off : Icons.visibility),
        ),
      ),
    );
  }
}

class TestWidget extends ConsumerWidget {
  const TestWidget({super.key});
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return ref.watch(streamProvider).when(
        data: (value) => const Text('TestWidget got data'),
        loading: () => const Text('TestWidget loading'),
        error: (error, stack) => Container());
  }
}

from riverpod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.