What is the difference between extends Object with Observable and extends Observable - dart

What is the difference between extends Object with Observable and extends Observable as applied to the class below.
The result is the same when the application is run.
library models;
import 'package:polymer/polymer.dart';
class Person extends Object with Observable {
#observable String name;
#observable bool signedAgreement = false;
Person();
Person.from(Person other) {
name = other.name;
signedAgreement = other.signedAgreement;
}
blank() {
name = '';
signedAgreement = false;
}
}
library models;
import 'package:polymer/polymer.dart';
class Person extends Observable {
#observable String name;
#observable bool signedAgreement = false;
Person();
Person.from(Person other) {
name = other.name;
signedAgreement = other.signedAgreement;
}
blank() {
name = '';
signedAgreement = false;
}
}

There no difference in the behaviour between this two declarations.
Here's a quote from Florian Loitsch :
When you extend "Object" with a mixin the first mixin can always take the place of "Object".
The only little difference is in class hierarchy (superclass are not the same) :
import 'dart:mirrors';
abstract class Mixin {}
class A extends Mixin {}
class B extends Object with Mixin {}
main() {
print(reflectClass(A).superclass);
// => ClassMirror on 'Mixin'
print(reflectClass(A).superclass.superclass);
// => ClassMirror on 'Object'
print(reflectClass(B).superclass);
// => ClassMirror on 'dart.core.Object with .Mixin'
print(reflectClass(B).superclass.superclass);
// => ClassMirror on 'Object'
}

Related

how to make a property is only readable from abstract class in Dart?

abstract class ListController {
int numberOfDocumentPerPage = 7;
}
then use the abstract class
class EventListController extends ListController {
}
but when I make an instance, I can change/assign the new value to the property
final controller = EventListController();
controller.numberOfDocumentPerPage = 1000; // should be error in here
so how to make a property is only readable from abstract class ?
UPDATE:
I also need to modify the property inside EventListController class
Basically you want the base class to declare a public getter. I think the simplest way would be:
abstract class ListController {
static const defaultNumberOfDocumentPerPage = 7;
int get numberOfDocumentPerPage => defaultNumberOfDocumentPerPage;
}
class EventListController extends ListController {
int _numberOfDocumentPerPage = ListController.defaultNumberOfDocumentPerPage;
#override
int get numberOfDocumentPerPage => _numberOfDocumentPerPage;
}
If you don't want the separate named constant, you could explicitly initialize your internal value with the result of calling the original getter:
abstract class ListController {
final int numberOfDocumentPerPage = 7;
}
class EventListController extends ListController {
EventListController() {
_numberOfDocumentPerPage = super.numberOfDocumentPerPage;
}
// ignore: super_in_invalid_context, https://github.com/dart-lang/sdk/issues/46850
late int _numberOfDocumentPerPage = super.numberOfDocumentPerPage;
#override
int get numberOfDocumentPerPage => _numberOfDocumentPerPage;
}
Another approach would be to provide a protected setter in the base class:
import 'package:meta/meta.dart';
abstract class ListController {
int _numberOfDocumentPerPage = 7;
int get numberOfDocumentPerPage => _numberOfDocumentPerPage;
#protected
set numberOfDocumentPerPage(int value) => _numberOfDocumentPerPage = value;
}
But note that the #protected annotation just provides a hint to the analyzer so that it can warn about violations; it is not enforced at compilation-time nor at runtime.

List property inheritance

The parser is complaining that the property inheritor .list is not subtype of ModelList.list, but LeadsGroup does inherit from Model.
Is this wrong? What is the correct way to do this?
My base class:
abstract class ModelList {
List<Model> get list;
set list(List<Model> n);
}
The inheritor:
class ListLeadsGroup extends ModelList {
List<LeadsGroup> list;
}
class LeadsGroup extends Model {
}
If you have code like
class Foo extends ModelList {}
ModelList ml = new ListLeadsGroup();
ml.list.add(new Foo());
ml.list is of type Model, therefore adding Foo should be legit.
But this very likely is not what you want.
This is why List<ListLeadsGroup> can't override List<Model>.
This should do what you want:
abstract class ModelList<T extends Model> {
List<T> get list;
set list(List<T> n);
}
class ListLeadsGroup extends ModelList<LeadsGroup> {
List<LeadsGroup> list;
}
class LeadsGroup extends Model {
}
just copied from Matan Lurey's comment on Gitter
import 'package:func/func.dart';
class ModelRegistry {
final _factories = <Type, Func0<Model>>{};
Model create(Type type) => _factories[type]();
void register(Type type, Model factory()) {
_factories[type] = factory;
}
}
main() {
var registry = new ModelRegistry();
registry.register(FooModel, () => new FooModel());
var foo = registry.create(FooModel);
}
found a solution using the new keyword covariant. Now the classes that extends ModelList can override the List<Model> list without warnings.
#serializable
abstract class ModelList extends ModifiedModel
implements Model {
Type get listType {
throw new UnimplementedError();
}
List<Model> get list;
set list(covariant List n);
}

Polymer Dart 1.0 - Observer not working

I have a custom element <test-object> which looks like this:
#PolymerRegister('test-object')
class TestObject extends PolymerElement with TestBehavior {
TestObject.created() : super.created() {
}
}
The TestBehavior:
#reflectable
class TestModel extends JsProxy {
#Property(notify: true)
num value = 0;
PolymerElement _target;
TestModel(PolymerElement target) {
_target = target;
}
changeBy(num by) {
value += by;
_target.set('testModel.value', value);
}
}
#behavior
abstract class TestBehavior implements PolymerBase {
#Property(notify: true)
TestModel testModel;
PolymerElement _instance;
static ready(instance) {
instance._init(instance);
}
static created(instance) {
instance._instance = instance;
}
_init(PolymerElement instance) {
set('testModel', new TestModel(instance));
}
}
My main app looks like this:
<dom-module id="main-app">
<style>
:host {
display: block;
#apply(--layout-center-center);
}
</style>
<template>
<div>{{testObject.testModel.value}}</div>
<test-object id="obj"></test-object>
</div>
</template>
</dom-module>
#PolymerRegister('main-app')
class MainApp extends PolymerElement {
#Property(notify: true)
TestObject testObject = null;
MainApp.created() : super.created();
ready() {
set('testObject', testObject = $$('#obj') as TestObject);
}
#Listen('click')
clicked([_]) {
testObject.testModel.changeBy(1);
}
#Observe('testObject.testModel.*')
valueChanged([_]) {
window.console.log('Value was changed');
}
}
This is a very simple toy example. I click <main-app> which causes the value in testModel to increase (this works). However, <main-app> doesn't update the div which should display the value. Also, valueChanged is never invoked.
I want to notify testObject about the valueupdate in testModel and propagate this notification up to main-app which than should update its UI via data binding.
Why is this not working?
You can't have a property in model classes, only in components
#Property(notify: true)
num value = 0;
should just be
#reflectable
num value = 0;
The set and notifyPath methods should really be called from your element classes, not the models. This actually simplifies your code a lot, and makes everything work as expected as well. Below I have pasted the new MainApp, TestModel, and TestBehavior classes (I also made some other minor edits).
#PolymerRegister('main-app')
class MainApp extends PolymerElement {
// Defines a read-only property (implicit because of the getter).
#property
TestObject get testObject => $['obj'];
MainApp.created() : super.created();
// I added the 2nd optional argument here, to fix a reflectable error
#Listen('click')
clicked([_, __]) {
// Notify directly here, this is the primary change.
notifyPath('testObject.testModel.value', testObject.testModel.changeBy(1));
}
#Observe('testObject.testModel.*')
valueChanged([_]) {
window.console.log('Value was changed');
}
}
// Removed the `_instance` field.
class TestModel extends JsProxy {
// use #reflectable instead of #Property(notify: true)
#reflectable
num value = 0;
TestModel();
num changeBy(num by) {
value += by;
// Added a return value for convenience
return value;
}
}
#behavior
abstract class TestBehavior implements PolymerBase {
#Property(notify: true)
TestModel testModel = new TestModel();
}

How do I add a valid default constructor to a dart polymer class?

The following code defines the Polymer element
What do I need as a valid default constructor for this class?
My question is what is needed for a proper constructor
import 'package:polymer/polymer.dart';
import 'lib/NPIDefs.dart';
import 'dart:html';
/**
* A Polymer click counter element.
*/
#CustomTag('detail-panel')
class NPIDetailPanel extends PolymerElement {
#published #observable NPIRecord record;
#observable String detailPanelICON = "unfold-less";
NPIDetailPanel.created() : super.created() {
}
setValue(NPIRecord npiRec) {
record = npiRec;
}
void dremoveDetailPanel() {
Element e;
e = shadowRoot.querySelector('#dpanel');
if(e != null) {
e.remove();
}
}
The code below gets a The class 'NPIDetailPanel' does not have a default constructor error
Please show how to do a default constructor
in the definition of the class
void addDetailPanel(Event e) {
NPIDetailPanel e1;
e1 = new NPIDetailPanel();
}
/* How do I add a proper default constructor? */
You can create a new instance of a Polymer element using new Element.tag('some-tag');
Just add a factory constructor that contains this to your Polymer element class.
#CustomTag('detail-panel')
class NPIDetailPanel extends PolymerElement {
factory NPIDetailPanel NPIDetailPanel() => new Element.tag('detail-panel'); // <== added
#published #observable NPIRecord record;
#observable String detailPanelICON = "unfold-less";
NPIDetailPanel.created() : super.created() {
}
setValue(NPIRecord npiRec) {
record = npiRec;
}
void dremoveDetailPanel() {
Element e;
e = shadowRoot.querySelector('#dpanel');
if(e != null) {
e.remove();
}
}
}
see also Instantiating polymer element via dart code

Forwarding an observable property from one object to another in Dart

I would like to detect a property change from one object and then forward that value (or recompute the value and pass the result) to another object's property. I saw the example from the documentation which demonstrates value forwarding:
class MyModel extends Observable {
StreamSubscription _sub;
MyOtherModel _otherModel;
MyModel() {
...
_sub = onPropertyChange(_otherModel, #value,
() => notifyPropertyChange(#prop, oldValue, newValue);
}
String get prop => _otherModel.value;
set prop(String value) { _otherModel.value = value; }
}
But I don't know where to get the oldValue and newValue from.
I suppose those should be passed as parameters to the callback of onPropertyChange (the third parameter), but that is not the case. The callback provides no parameters. Is this an oversight or am I missing something ?
import 'dart:async';
import 'package:observe/observe.dart';
class MyOtherModel extends Object with Observable {
#observable
String value;
}
class MyModel extends Object with Observable {
StreamSubscription _sub;
MyOtherModel _otherModel = new MyOtherModel();
MyModel() {
///...
_otherModel.changes.listen((crs) {
crs.forEach((PropertyChangeRecord cr) =>
notifyPropertyChange(#prop, cr.oldValue, cr.newValue));
});
}
String get prop => _otherModel.value;
set prop(String value) => _otherModel.value = value;
}
void main() {
MyModel m = new MyModel();
m.prop = 'bla';
m.changes.listen(print);
// initiate change notification
Observable.dirtyCheck();
}
output
[#<PropertyChangeRecord Symbol("value") from: null to: bla>]

Resources