Coder Social home page Coder Social logo

Comments (8)

escamoteur avatar escamoteur commented on July 17, 2024

Can you please the code of of your Widget please?

from get_it_mixin.

rayliverified avatar rayliverified commented on July 17, 2024

Will add test project.

from get_it_mixin.

rayliverified avatar rayliverified commented on July 17, 2024

I've created a sample project at https://github.com/searchy2/get_it_mixin/tree/ray-streambuilderissue
The file with the StreamBuilder issue is at main_streambuilder.dart.

There are multiple issues with StreamBuilders that are quite confusing. I've isolated the issues as best as possible.

Issue 1: WatchX Outside Stream builder fails on hot reload rebuild

https://www.youtube.com/watch?v=7asv6UfZRbY

StreamBuilder Hot Restart Issue
class StreamBuilderHotReloadIssue extends StatefulWidget {
  @override
  _StreamBuilderHotReloadIssueState createState() =>
      _StreamBuilderHotReloadIssueState();
}

class _StreamBuilderHotReloadIssueState
    extends State<StreamBuilderHotReloadIssue> {
  static StreamController<int> counterController = StreamController<int>();
  Stream<int> counter = counterController.stream;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 1000), () {
      counterController.add(1);
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GetItCounterOutsideStreamBuilder(),
        StreamBuilder(
          stream: counter,
          builder: (context, snapshot) {
            print('Build Stream');
            if (!snapshot.hasData) return SizedBox.shrink();
            int counterValue = snapshot.data as int;
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text('Stream Counter: ' + counterValue.toString()),
                GetItCounterInStreamBuilder(),
              ],
            );
          },
        ),
      ],
    );
  }
}

Issue 2: Stream Rebuild Breaks WatchX Rebuilds.

ezgif com-video-to-gif (1)

StreamBuilder Rebuild Issue
class StreamBuilderRebuildIssue extends StatefulWidget
    with GetItStatefulWidgetMixin {
  @override
  _StreamBuilderRebuildIssueState createState() =>
      _StreamBuilderRebuildIssueState();
}

class _StreamBuilderRebuildIssueState extends State<StreamBuilderRebuildIssue>
    with GetItStateMixin {
  static StreamController<int> counterController = StreamController<int>();
  Stream<int> counter = counterController.stream;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 1000), () {
      counterController.add(1);
    });
    // This triggers a StreamBuilder rebuild and breaks WatchX.
    Future.delayed(Duration(milliseconds: 2000), () {
      counterController.add(2);
    });
  }

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

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        StreamBuilder(
          stream: counter,
          builder: (context, snapshot) {
            print('Build Stream');
            if (!snapshot.hasData) return SizedBox.shrink();
            int counterValue = snapshot.data as int;
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text('Stream Counter: ' + counterValue.toString()),
                GetItCounterInStreamBuilder(),
                GetItCounterOutsideStreamBuilder(),
              ],
            );
          },
        ),
      ],
    );
  }
}

from get_it_mixin.

escamoteur avatar escamoteur commented on July 17, 2024

Actually the latter makes completely sense to be because the builder has it's own context. So probably won't work together with the mixin or wit hooks. So better don't mix them inside one widget tree.

from get_it_mixin.

escamoteur avatar escamoteur commented on July 17, 2024

Just confirmed with Remi. My hunch was right. it can't work inside most builders. And the idea of the mixin is not to have to use builders to make you code more readable.

from get_it_mixin.

escamoteur avatar escamoteur commented on July 17, 2024

Concerning the hot reload I have to look.

from get_it_mixin.

escamoteur avatar escamoteur commented on July 17, 2024

I fixed the Hot reload. Please give it a try. And I added to the readme that you can't use it with Builders

from get_it_mixin.

rayliverified avatar rayliverified commented on July 17, 2024

Amazing, will test and confirm!

from get_it_mixin.

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.