-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move Listenable
, ValueListenable
, ChangeNotifier
and ValueNotifier
to the Dart sdk and add listening methods
#55816
Comments
Listenable
, ValueListenable
, ChangeNotifier and
ValueNotifier` to the Dart sdk and add listening methodsListenable
, ValueListenable
, ChangeNotifier
and ValueNotifier
to the Dart sdk and add listening methods
Could it be a stream, or is the API defined to work synchronously? (Or is it just that it has no errors and no done events?) I can see the advantage of getting the subscription passed to every callback, it's something stream listeners have work to get access to. (I could see myself add an extension That said, this is an API that can easily be implemented by a package, and something I'm not personally particularly invested in, so I don't see it becoming a high prioirty. Especially if it's hard to migrate existing Flutter code to the new implementation. |
@lrhn to give some context.
They implement the Observer pattern and work synchronously. They are used everywhere in the Flutter Framework and are very popular for state management solutions. My package Especially with the advent of Dart on the server people want to use them outside of a Flutter context but can't because they are part of the Flutter SDK. So you have to write your own which isn't compatible to the Flutter one because not even the interfaces are of the Dart SDK. Currently, you are not able to publish any package that uses Some friends and I spent considerable time optimizing the I would keep the existing API, maybe make I propose to add some more non-breaking features that I implemented in For
For ValueNotifiers: From my CustomValueNotifier: /// Sometimes you want a ValueNotifier where you can control when its
/// listeners are notified. With the `CustomValueNotifier` you can do this:
/// If you pass [CustomNotifierMode.always] for the [mode] parameter,
/// `notifierListeners` will be called every time you assign a value to the
/// [value] property independent of if the value is different from the
/// previous one. This can be useful when you want to trigger a rebuild everytime you
/// received ne data for instance even if the data hasn't changed.
/// If you pass [CustomNotifierMode.manual] for the [mode] parameter,
/// `notifierListeners` will not be called when you assign a value to the
/// [value] property. You have to call it manually to notify the Listeners.
/// Additionally it has a [listenerCount] property that tells you how many
/// Listeners are currently listening to the notifier. further additions could be
@kevmoo might be interested in this too as he recently asked about sharing business logic between Flutter and non Flutter web frameworks |
Another option @escamoteur would be to publish this as a package. I don't there is a reason to have them in Dart itself...other than to avoid a package version dance. |
The problem is that you cannot write a pure Dart package using this types. If you implement them yourself they are not compatible to Flutter. At least the interfaces should be inside the Dart sdk.
Am 23. Mai 2024, 19:45 +0200 schrieb Kevin Moore ***@***.***>:
… Another option @escamoteur would be to publish this as a package. I don't there is a reason to have them in Dart itself...other than to avoid a package version dance.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
So the problem is that not only are the interfaces declared by the Flutter SDK, it's also used by it, which is why it can't come from a package. |
Could we create a package for these interfaces that uses conditional imports to import the flutter versions in flutter That'd work, right? |
...turns out, it won't work because you'd still need a |
@kevmoo I mentioned on X that I had filed or joined Flutter tickets related to moving I will offer a couple examples where geometry was relevant. First, years ago I created a geometry Dart package: https://pub.dev/packages/superdeclarative_geometry - that package couldn't operate in terms of Flutter geometry concepts because there weren't any Dart-level APIs for that. Second, a year go I was working on direct PDF rendering with Flutter. But we wanted to create a pure Dart PDF parser. Due to the vector-graphics-style encoding of PDFs, the concept of offsets, rectangles, lines, and shapes are baked into the format. The desire to create a pure Dart parser, combined with a Flutter renderer, resulted in other unnecessary translations from a Dart rectangle to a Flutter rectangle, and a Dart offset to a Flutter offset. These are just two that I remember now in the moment. I think I've run into similar frustrations more times than that. BTW, a thought on packaging. I think you mentioned something like For example, we already have at least two completely unrelated groups of things in Flutter that we'd like in Dart: observables and geometry. I think it would be a mistake to throw both of those in the same package just because they originally came from Flutter. I think it would make a lot more sense to have |
@matthew-carroll – thanks for the context. Yeah, |
Is the SDK definitely the right place? Perhaps this is another wrinkle to at least keep mind. It seems like the primary goal is to be able to use the same APIs in pure Dart and Flutter for various things. This goal doesn't technically require that those things live in the Dart SDK. They could also be Dart team packages on Pub and still meet that goal equally well. I'm sure there are other implications there, but I just wanted to make it clear that at least for any cases I've been concerned with, I had no need for those things to actually come baked into the language SDK. |
@matthew-carroll – right again. Generally, we've REALLY avoided characters: 1.3.0
collection: 1.18.0
material_color_utilities: 0.11.1
meta: 1.14.0
vector_math: 2.1.4 But I guess adding one more that (effectively) NEVER changes would be easier. |
Are there any objective metrics or direct cost analysis to depending on packages? I think we can all understand and appreciate that the extreme of Flutter depending on LOTs of packages is bad. We can all imagine a constant churn as dozens of upstream packages change and cause Flutter version rev'ing constantly. We certainly don't want that. But, similarly, taking a stance of "adding any more packages is necessarily bad/harmful/costly" probably isn't ideal for engineering, either. For example, Dart-team controlled packages are a completely different beast than community packages. The Dart team, which in many regards is interchangeable with the Flutter team, can make decisions about when those packages are versioned, and can also enforce very strong backwards compatibility and semantic versioning practices. So even if you added 10 more packages from the Dart team, it's not clear at all that the state of affairs would be any worse than they are currently. This is doubly true if we're talking about adding packages to Flutter which are defined as existing Flutter APIs that are extracted to packages. That code already lives in Flutter, and Flutter already owned the consequences of that code changing. In other words, if you extract something like |
One reason why moving these packages into the Dart sdk in my opinion is, that it would keep existing Flutter apps compile as they do know without needing anyone to change anything because Observables as Geometry aren't imported as dedicated packages in Flutter apps.
Both, Observables and geometry are so generally usable that they would make a great addition for any Dart developer outside of Flutter.
Am 29. Mai 2024, 02:17 +0200 schrieb Kevin Moore ***@***.***>:
… @matthew-carroll – right again. Generally, we've REALLY avoided pkg:flutter depending on any other packages.
characters: 1.3.0
collection: 1.18.0
material_color_utilities: 0.11.1
meta: 1.14.0
vector_math: 2.1.4
But I guess adding one more that (effectively) NEVER changes would be easier.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@escamoteur reminds me of a related detail. @kevmoo what are your thoughts about the ability to re-export a package that you import? There are certain vocal developers who think Flutter should solve all sorts of app problems. When pushed for specific reasons why, one reason that came up was that they were frustrated that there's no canonical package for each app requirement, e.g., payments, push notifications, database, etc. I said that the community should solve this by creating a meta package which makes an opinionated selection of the best mobile app-centric packages in one. But then I realized that technically that can't happen because I don't think we can re-export packages that we imported, right? If Dart made it possible to re-export the dependency packages then we could provide that meta package aimed at quickly shipping mobile apps. But also, the problem that @escamoteur wouldn't be a problem because Flutter could re-export the |
You mean something like the following? If so, I believe that should already work. library foo;
// Library foo exports all of library `geometry`.
export 'package:geometry/geometry.dart'; |
@felangel I thought I tried that and it didn't work, but maybe it does. Maybe I got something wrong in the syntax. Doing this with many packages might benefit from a pubspec level re-export. But if that |
Yeah afaik that should already work and yeah flutter could just re-export library foundation;
export 'package:observable/observable.dart' show
Listenable,
ChangeNotifier,
ValueListenable,
ValueNotifier; |
I can see a problem here. If I were to make a new Moving the code into a |
Upvote on extracting observable / geometry packages, and keep it in the hands of dart team or flutter team. But I'm against adding it to Dart SDK |
A few thoughts: A. It looks like bundling into one issue both the listening method changes and pulling the API out of Flutter so it can be used in Dart contexts has confused the conversation. Maybe we should have a separate issue for the listening method changes and edit the title and original issue explaining that that part of the conversation has been moved elsewhere? B. Being able to use The Listenable/Notifier API in pure Dart would be super helpful. For me, I like using the API for my core state management primitives. Many state management packages like Riverpod have their own equivalents to
|
Agree @caseycrogers – this issue is PURELY about moving EXISTING Flutter types somewhere they can be shared. We DO NOT want to confuse that with (very valid) discussions about using |
Sorry for the churn here. Closing this request out in favor of flutter/flutter#149466 The mentioned types in this issue in particular would likely be moved to a (new) package, not the Dart SDK. Instead of renaming everything, we'll start with the design issue and open up implementation issues if/when we decide to move forward. Thanks for the inspiration @escamoteur |
There have been several cases where developers had to reimplement these classes because they didn't want a reference to flutter in their package. As none of these classes needs anything from the Flutter SDK but they are all pretty handy it would make sense to move them from the Flutter to the Dart SDK.
At the same time, their API could be greatly improved by giving them a Stream-like
listen
method:and for ValueListenable
to no longer having to use
addListener
andremoveListener
where you have to make sure, to store a reference of your handler.Instead of calling
removeListener
you callcancel()
on the returned subscription object.The reason why the subscription is passed to the handler function too is that the handler will be able to
cancel()
the subscription itself if needed.my package functional_listeners implements this listen function as an extension method and it has proven how much nicer it makes the usage of
Listenables
The text was updated successfully, but these errors were encountered: