Using Mixins in flutter - dart

I am not able to use mixins in my flutter app, It says:
The class 'UserModel' can't be used as a mixin because it extends a class other than Object

It wasn't officially possible to have mixins with that extends something for a long time.
It has been recently enabled, but with another syntax:
mixin UserModel on Model {
// do some stuff
}

Related

Vaadin 23 cannot handle #Viewport annotation

I have upgraded a Vaadin 14 app to Vaadin 23.1.3 and now it refuses to start with this error message:
Found app shell configuration annotations in non `AppShellConfigurator` classes.
Please create a custom class implementing `AppShellConfigurator` and move the following annotations to it:
- #Viewport from my.custom.class
However, my class does not have this annotation, it only extends an abstract class with this annoation. But I cannot change the abstract class, because it is a third party lib. So is there a way to tell Vaadin to just ignore this annotation and use the one I provided in the AppShellConfigurator class? Or any other way to get this app to start short of ditching the 3rd party library?
If the problematic class is just an abstract one, You can try creating a custom class with exactly the same structure and name but without the Viewport annotation. Then just import your custom class instead of the one from external package.
For example while using the AppLayoutRouterLayout class from com.github.appreciated:app-layout-addon package You could replace its usage with following class:
import com.github.appreciated.app.layout.component.applayout.AppLayout;
import com.github.appreciated.app.layout.component.router.AppLayoutRouterLayoutBase;
public abstract class AppLayoutRouterLayout<T extends AppLayout> extends AppLayoutRouterLayoutBase<T> {
}

Dart - Hide method from parent class / call private method from parent class

I have a class that extends another. The parent class is not intended to be used directly by the API rather it implements basic methods that are helpers for child classes.
When I use the child class in a program I can see all method from said class but also the one from the parent class that are not intended to be called directly, they exist to be called by the methods of the child class.
I tried to make parents method private. This would work I believe as long as parent and child are declared in the same library. But I have an issue with the "library" notion. I understand part/part of are somewhat depreciated, and I want the parent class to be in a specific file. I can't figure a way to do it with import/export.
Is there a way to either hide a public method from the parent class from all child classes usage or to make a private method from the parent class callable from all child classes ?
Best regards ;
Exemple:
myLib.dart
export mainClass.dart;
mainClass.dar
import baseClass.dart;
class MainClass extends BaseClass {
publicFunc() => ... //Can't call _hiddenFunc, can call wantToHideFunc()
}
In a second file (for code reusability purposes)
class MainClass extends BaseClass {
_hiddenFunc() => ...
wantToHideFunc() => ...
}
Using myLib public API
import myLib.dart
main() {
class = Class();
class.publicFunc(); //Intended
class.wantToHideFunc() //Wants to avoid...
}
Dart does not have protected access like Java or C#.
You can't hide a public member, you can't access a private member from a different library, and there is no third option.
It sounds like you want members of the superclass which can be invoked only from subclasses, not from outside of the object. That's what's called protected in, e.g., Java, and Dart does not have anything similar to that.
The only level of privacy in Dart is library-private, which is chosen by starting the name with a _.
The reason that Dart has this design is that it was originally a very dynamic language. You can preform "dynamic invocations" on a value with static type dynamic, say dynVal.foo(42) and it will call the method of that name.
To make a name unreachable, it needed to be safe from dynamic invocation as well. Because of that, Dart privacy does not care where the code doing the invocation is, it only cares whether you know the name - and library private names are considered different names depending on which library they're from.
Using part is not discouraged for situations where it actually serves a purpose. If you can put the part into a library of its own, that's better because it allows it to have its own privacy and imports, but if you need the classes to share privacy, using part files to split up a large file is perfectly reasonable. It's a tool, there is nothing wrong with using it when it's the right tool for the job. A library is often a better tool for modularity, but not always.
Now, there is a hack you can use:
// Declare the base and provide extensions for "protected" members:
abstract class Base {
int get _someHiddenStuff => 42;
int get somePublicStuff => 37;
}
extension ProtectedBase on Base {
int get someHiddenStuff => _someHiddenStuff;
}
Then import that in another library and do:
import "base.dart";
export "base.dart" hide ProtectedBase;
class SubClass extends Base {
int doSomething => someHiddenStuff + somePublicStuff;
}
Then anyone importing "subclass.dart" will also get a version of Base, but they won't get the ProtectedBase extensions. Hiding the extensions from your package's public API will allow yourself to use it, but prevent your package's users from seeing the helper extensions.
(This is likely highly over-engineered, but it's an option. It's the evolution of the hack of having static/top-level helper functions that you don't export.)

Extending dart class similar to js prototyping or swift extension

Extending class that provided to me. Giving a new functionality to already existing classes. Or extending already existing mixins or virtual classes, anything can work.
Maybe something like:
class FlatButton {} // maybe not defined by me
mixin on FlatButton {
roundCorner(int pixels) {
//...
}
}
final button = FlatButton();
button.roundCorner(10)
Swift/Kotlin support this by extension keyword or js with prototype.
There is an open issue about this on dartlang repo. It is still open and don't think it is possible for now.

"extends" versus "implements" versus "with"

I want to understand the difference between extends, implements and with. When do I use each keyword?
Extends:
Use extends to create a subclass, and super to refer to the superclass.
Extends is the typical OOP class inheritance. If class a extends class b all properties, variables, functions implemented in class b are also available in class a. Additionally you can override functions etc.
You use extend if you want to create a more specific version of a class. For example the class car could extend the class vehicle. In Dart a class can only extend one class.
Implements:
Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements. If you want to create a class A that supports class B’s API without inheriting B’s implementation, class A should implement the B interface.
Implements can be used if you want to create your own implementation of another class or interface. When class a implements class b. All functions defined in class b must be implemented.
When you're implementing another class, you do not inherit code from the class. You only inherit the type. In Dart you can use the implements keyword with multiple classes or interfaces.
With (Mixins):
Mixins are a way of reusing a class’s code in multiple class hierarchies.
With is used to include Mixins. A mixin is a different type of structure, which can only be used with the keyword with.
They are used in Flutter to include common code snippets. A common used Mixin is the SingleTickerProviderStateMixin.
extend can only be used with a single class at the time, BUT... you can easily extend a class which extends another class which extends another class which...! ;)
In fact, most Flutter widgets are already built like that.

ActionScript - Class Extends Object?

i've been poking around RIM's PlayBook SDK and noticed they extend Object on a lot of their classes.
are there benefits to extending Object on a custom class that would normally be unextended?
package qnx.notificationManager
{
public class Notification extends Object
{
...
If you do not specify that your class extends another class, the compiler automatically makes your class extend Object. So the answer is that you don't gain any benefit; they must have just been trying to be explicit, or maybe the code was generated through decompilation.

Resources