Is there a way to combine multiple meta annotations into one? - flutter

At the moment we have a lot of classes in our codebase, who are using the same meta annotations. An example would be:
#autoEquatable
#immutable
#jsonSerializable
class SignInWithFacebookEvent extends LoginEvent {}
Is there a way in Dart where I can combine those into one? For example:
#event
class SignInWithFacebookEvent extends LoginEvent {}
This would be much simpler, and also if I would need to add/edit/remove another meta annotation from all event classes, I could just add/edit/remove one without editing hundreds of class.
Edit:
Even better would be inheritence of meta annotations for example:
#autoEquatable
#immutable
#jsonSerializable
class LoginEvent {}
And all of the sub classes have those annotations to. Is something like this possible?

Related

What is the difference between class and mixin in dart?

What is the difference between:
class A {}
class B with A{}
and
mixin A{}
class B with A{}
?
In Dart, a class can only extend one other class. But it can implement or mixin as many as you want.
The thing is, when you extend a class, you inherit all its attributes, methods and it's constructors. When you implement a class, if you are adding only methods/attributes that you don't already have, you can simply continue your code. If you are implementing an abstract method, you'll need to actually implement it. Now, mixins are like extended classes, the classes that mix them, are their child as well as with extend and implements, but it has no constructor.
The actual idea for mixins is that you can add functionalities to any class, and they don't have to extend another class. That's why they usually do simple stuff only.

Dart multiple inheritance?

I have two classes: ChildOne and ChildTwo extended from ParentClass. ChildTwo has a custom field over the inherited ones.
In some parts of the app, most of the time this ParentClass is used to display data to UI and some widgets need to know if the instance is ChildTwo to display the custom field.
However in another part of the app, I have to enhance the ParentClass, so I created another class EnhancedParentClass extends ParentClass.
This class use used in most of the widgets, but again there are some widgets that need to know the difference between the EnhancedParentClass and let's say an extended class ChildTwoEnhancedClass.
What’s the best way of solving this?
class ChildTwoEnhancedClass extends EnhancedParentClass implements ChildTwo
class ChildTwoEnhancedClass extends ChildTwo implements EnhancedParentClass
Is there a way to do this, without the need to extend the main classes “in paralel” with the “enhanced” one? Or is it something completely wrong with this implementation?
I looked over mixins, but I don’t think they fit this case, as far as I’ve seen mixins are used to share logic(mimic multiple inheritance), not necessarily share class members.
These classes are used as data immutable models.

Why stateful/stateless widgets extends and doesn't implements?

I have this question for a job interview, and i want to know why the stateful and staless widgets, use extends and why not use implements in Flutter/Dart?
Extends
extends is usually the go-to when you want to make a more specific version of a class.
When you extend a class, you inherit all properties, methods, etc.
If you want to override a method, you prefix the method with #override.
Implements
implements is when you want to make a whole new version of a class but you want to inherit the class type.
When you create a new implementation of a class, you are responsible for supplying everything necessary to make that class function.

Should you use "extends" or "with" keyword for ChangeNotifier? - Flutter

I have seen several examples of a model extending ChangeNotifier using both 'extends' and 'with' keywords. I am not sure what the difference is.
class myModel extends ChangeNotifier {...}
class myModel with ChangeNotifier {...}
What is the difference between those two? Which one should I use?
You can use either extends (to inherit) or with (as a mixin). Both ways give you access to the notifyListeners() method in ChangeNotifier.
Inheritance
Extending ChangeNotifier means that ChangeNotifier is the super class.
class MyModel extends ChangeNotifier {
String someValue = 'Hello';
void doSomething(String value) {
someValue = value;
notifyListeners();
}
}
If your model class is already extending another class, then you can't extend ChangeNotifier because Dart does not allow multiple inheritance. In this case you must use a mixin.
Mixin
A mixin allows you to use the concrete methods of the mixin class (ie, notifyListeners()).
class MyModel with ChangeNotifier {
String someValue = 'Hello';
void doSomething(String value) {
someValue = value;
notifyListeners();
}
}
So even if your model already extends from another class, you can still "mix in" ChangeNotifier.
class MyModel extends SomeOtherClass with ChangeNotifier {
String someValue = 'Hello';
void doSomething(String value) {
someValue = value;
notifyListeners();
}
}
Here are some good reads about mixins:
Dart: What are mixins?
Dart for Flutter : Mixins in Dart
extends is used to inherit a class
with is used to use class as a mixin
Refer here to understand difference between mixin and inheritence: https://stackoverflow.com/a/860312/10471480
Refering to ChangeNotifier, the docs says
A class that can be extended or mixed in that provides a change notification API using VoidCallback for notifications.
Hence you can inherit it as well as use it as a mixin
I think this has to do with legacy support. The mixin keyword was introduced in Dart 2.1.0. According to the documentation, you should AVOID mixing in a type that isn’t intended to be a mixin:
For compatibility reasons, Dart still allows you to mix in classes
that aren’t defined using mixin. But, that’s risky. If the author of
the class doesn’t intend the class to be used as a mixin, they might
change the class in a way that breaks the mixin restrictions. For
example, if they add a constructor, your class will break.
If the class doesn’t have a doc comment or an obvious name like
IterableMixin, assume you cannot mix in the class if it isn’t declared
using mixin.
As Dart does not allow multiple inheritances the ChangeNotifier API allows it to be mixed in. As the documentation states:
A class that can be extended or mixed in that provides a change
notification API using VoidCallback for notifications.
IMHO to be aligned to the language specification Flutter should change the ChangeNotifier, separating those reusable implementations into Mixin Classes. At least the Dart documentation makes me think this is something that could not be supported anymore in a future release [the mix in of a non mixin class].
There are lot of discussions around this:
Make ChangeNotifier a mixin
Allow mixins in "extends" clauses
in Provider, Both allow to use notifyListeners(),But
Extend means that (ChangeNotifier) is superclass,
Mixin allows using the methods from (ChangeNotifier) class
also
You can extend one class
But can mixin more than one class
This is a good short article about difference:
Should use Extends or Mixin | Flutter Tips

Eclipse RCP - Custom FilteredPreferenceDialog

I'm trying to implement a custom preference dialog using the FilteredPreferenceDialog class.
The problem that this is an abstract class, but I dont really understand why. It has no abstract methods. I created my own class which extends FilteredPreferenceDialog but then I get the discouraged access warnings. There is another class called WorkbenchPreferenceDialog which also extends FilteredPreferenceDialog, and its also abstract.
Is there a class which is a public not abstract class I can create which has the filtering implementation? The PreferenceDialog class works fine except it doesnt have the filtering mechanism.