3

Why use InheritedWidget while we can use Broadcast Streams | StreamBuilder and Static Variables?

Why should we bother about redux, scoped model, etc. while we can have a simple and clean architecture?

enter image description here

aliep
  • 1,702
  • 2
  • 21
  • 33

1 Answers1

2

Streams/Sink definitely are excellent to store a state. There are some existing architectures, such as BLoC which uses them a lot.

But, Streams don't entirely replace InheritedWidget either. InheritedWidget comes with the cool ability to override it's content for only a part of the screen. One cool application of this is Theme.

Generally speaking, Streams are cool to store business logic. But when you need to store UI logic, InheritedWidgets takes the upper hand.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • Thanks, I'm not looking for a replacement, I'm looking for a simple and clean architecture – aliep Apr 21 '18 at 18:58
  • when I'm using a StreamBuilder, I'm actually overriding the content for only a part of the screen, Am I wrong? – aliep Apr 21 '18 at 19:00
  • You're wrong. `InheritedWidget` pass info to it's children. While stream is **inside** the widget. So in the case of `InheritedWidget`, other widgets can have influence. While with streams they can't – Rémi Rousselet Apr 21 '18 at 19:02
  • The stream is not inside the widget, the stream is broadcast and is global to the application. there's no need to pass down anything down the hierarchy, just store the state in a global/static variable – aliep Apr 21 '18 at 19:05
  • But you share the same stream. While you instantiate multiple inheritedwidget. Just try to reproduce `Theme` with streams, you'll easily understand – Rémi Rousselet Apr 21 '18 at 19:06
  • I know what your'e talking about, changing the theme forces to redraw all the widgets, but subscribing every widget to a stream is a PITA – aliep Apr 21 '18 at 19:09
  • There's more to what Remi is saying (although Remi this might be a language thing for you, but saying "you're wrong" as the first thing in a sentence is generally a good way to make the person ignore the rest of what you're about to say and assume you're an ass.) You can have different parts of your application use different themes without having to set up different subscriptions simply by defining more than one theme - which is quite powerful. Also, in general any architecture that relies strongly on static variables is probably not optimal. – rmtmckenzie Apr 21 '18 at 20:49
  • Especially once you start doing things like unit testing, promoting code re-use, and trying to follow clean code principles you'll realize how using static subscriptions makes things difficult. For a very small application it could save you time but in the long run it can produce harder to maintain code. – rmtmckenzie Apr 21 '18 at 20:51
  • I don't think what you're talking about is in the scope of the question @rmtmckenzie. The question is "why use inheritedwidget over stream?" not "should I base my app around streams". – Rémi Rousselet Apr 21 '18 at 21:04
  • But I personally think BLoC (aka stream everywhere) is a decent architecture. – Rémi Rousselet Apr 21 '18 at 21:06
  • Could you please explain what is `business logic` and what is `UI logic`? – Muhammad May 20 '20 at 18:21