Dart - Map with Set as key - dart

I'm trying to construct a Map object with Set as keys:
final myObject = <Set<MyType1>, MyType2>{};
I can successfully insert new values in my object
myObject[{myType1}] = myType2;
However, I cannot get back anything from it as 2 sets with the same values won't be consider equal:
{0} == {0}; // <- false
myObject[{myType1}]; // <- null
Is there something built-in that could allow me to use a Map<Set<T>, U> object?

You may create your own class that wraps a Set with your own equals and hashCode. Then the problem is solved.
A very rough example to demonstrate my idea:
class MySet<T> {
final Set<T> inner;
...
#override
bool equals(other) => setEquals(this, other); // https://api.flutter.dev/flutter/foundation/setEquals.html
#override
int hashCode => ...
}
P.S. The equals/hashCode of a Set is not implemented as whether the contents are the same, because that would be very costly.

Related

Dart Hive TypeAdapter rational for write() method

I am trying to understand the rational behind using writeByte(3) in the write method in Hive TypeAdapter.
Please consider the class:
#HiveType()
class Person{
#HiveField(0)
String name;
#HiveField(1)
int age;
}
In the TypeAdapter below It is easy to understand the read method, since it is just reads sequentially each field.
However, I'm trying to figure out why the same mechanism does not apply to the write, instead of using ..writeByte(...) just before each field. And, what is the meaning of the first ..writeByte(2)?
class PersonAdapter extends TypeAdapter<Person> {
#override
Person read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int, dynamic>{
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Trips()
..name = fields[0] as String
..age = fields[1] as int;
}
#override
void write(BinaryWriter writer, Person obj) {
writer
..writeByte(2) // Why this here? (sometimes I see writeByte(3) !! )
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.age);
}
}
Thanks for any clarification.
I know nothing about Hive but if you take a look at the builder which create this write method you can see the following:
String buildWrite() {
var code = StringBuffer();
code.writeln('writer');
code.writeln('..writeByte(${getters.length})');
for (var field in getters) {
var value = _convertIterable(field.type, 'obj.${field.name}');
code.writeln('''
..writeByte(${field.index})
..write($value)''');
}
code.writeln(';');
return code.toString();
}
https://github.com/hivedb/hive/blob/59ad5403593283233d922f62f76832c64fa33a3b/hive_generator/lib/src/class_builder.dart#L122
So based on this we can conclude the first writeByte is the length of getters. The next one is the index of the first getter (0) following by the value and next getter (1) with value and so on.
This makes sense since the protocol properly needs to know how many fields it can expect to get.

Dart "upcasting" is not actually upcasting

I am trying to up-cast the subclass object but it is not working.
The following program compiles without any errors.
VideoStreamModel model = VideoStreamModel("");
VideoStream entity = model;
print(model); // prints VideoStreamModel
print(entity); // prints VideoStreamModel
print(entity as VideoStream); // prints VideoStreamModel
print(cast<VideoStream>(model)); // prints VideoStreamModel
I have written a testcase to test the relation of above two classes and it passes.
test('should be a subtype of VideoStream', () async {
expect(model, isA<VideoStream>());
});
What could be the problem here?
EDIT:
[deleted]
EDIT 2:
[deleted]
Edit 3:
Here is the complete code reproducing the error.
import 'package:equatable/equatable.dart';
import 'package:test/test.dart';
class A extends Equatable {
final String x;
A(this.x);
#override
List<Object> get props => [x];
}
class B extends A {
B(String x) : super(x);
A method() {
B b = B(x); // doing A b = A(x) makes the test pass
return b;
}
}
void main() {
B b = B("");
test('test', () async {
final expected = A(b.x);
final actual = b.method();
expect(actual, expected);
});
}
It generates the following assertion error:
Expected: A:<A>
Actual: B:<B>
print is calling the toString() on the object you are pointing at (in this case VideoStreamModel) which knows what type it is. When you are casting, you are not changing anything about the object itself but only how the compiler should see the object when it determines if you are allowed to use a given typed variable to point to the object.
So when you are doing entity as VideoStream you are really just telling the compiler that you "promise" that the entity can be seen as a VideoStream. But on runtime, this cast will be tested to see if it is true.
All of this is really not an issue since you should never test for the specific type of the object when you are programming Dart but instead use the is operator which tests if a given object is compatible with a given interface.
So e,g, (entity is VideoStream) will return true.
Updated part
You problem seems to be a misunderstanding of the use of Equatable. It is important to notice that Equatable are not only using the elements from props to determine if two objects are equal but it also looks at the runtimeType. You can see this from the implementation:
#override
bool operator ==(Object other) =>
identical(this, other) ||
other is Equatable &&
runtimeType == other.runtimeType &&
equals(props, other.props);
https://github.com/felangel/equatable/blob/master/lib/src/equatable.dart#L46
This means that:
A a = A("");
B b = B("");
print(a == b); // false
When you are using expect without any matcher, it will just make an == operation which is stated in the documentation:
matcher can be a value in which case it will be wrapped in an equals matcher
Since we (as stated before) cannot change the runtimeType of an object after its creation you need to implement your own == if you want the two object instances to be seen as equal since equatable does only see two objects as equal if they both is created from the same class and contains the same values defined with props.

How can you map/"resink" the input side of a StreamController

Say I have a StreamController<int> called myController. I understand that I can map the output of the stream by doing something like Stream<int> intStream = myController.map((i) => i * 2) and then listen to that.
But what if I wanted to intercept/map the input of the sink, how would I achieve this? Is there an API for this? For clarity, have a look at this fake non-working example: Sink<bool> boolSink = myController.sink.resink<int>((bool b) => b ? 1 : 0). So a mapping that takes a sink of bools and converts it to a sink of ints. Then I would do boolSink.add(true) and expect intStream to emit 1.
My goal is to provide simplified sinks (Sink<void>) to a few components, but without them having to place the particular value into the sink (because they will each only ever be adding a predefined value). And ideally without having to manage multiple StreamControllers myself.
My solution was to have a class which implements the Sink interface. It intercepts calls to the add method, maps the value being added to a new value, and then forwards it on to the "real" sink. I need a new instance of the class for each type of mapping.
import 'package:meta/meta.dart';
typedef Mapper<T, R> = R Function(T);
/// A [Sink] which takes a value of type [T] and maps it to a destination sink
/// which expects a value of type [R].
class SinkMapper<T, R> implements Sink<T> {
final Mapper<T, R> _mapper;
final Sink<R> _sink;
SinkMapper({
#required Mapper<T, R> mapper,
#required Sink<R> sink,
}) : _mapper = mapper,
_sink = sink;
#override
void add(T data) => _sink.add(_mapper(data));
#override
void close() => _sink.close();
}

How do I compare two objects to see if they are the same instance, in Dart?

Say I have a class that has many instance variables,. I want to overload the == operator (and hashCode) so I can use instances as keys in maps.
class Foo {
int a;
int b;
SomeClass c;
SomeOtherClass d;
// etc.
bool operator==(Foo other) {
// Long calculation involving a, b, c, d etc.
}
}
The comparison calculation may be expensive, so I want to check if other is the same instance as this before making that calculation.
How do I invoke the == operator provided by the Object class to do this ?
You're looking for "identical", which will check if 2 instances are the same.
identical(this, other);
A more detailed example?
class Person {
String ssn;
String name;
Person(this.ssn, this.name);
// Define that two persons are equal if their SSNs are equal
bool operator ==(Person other) {
return (other.ssn == ssn);
}
}
main() {
var bob = new Person('111', 'Bob');
var robert = new Person('111', 'Robert');
print(bob == robert); // true
print(identical(bob, robert)); // false, because these are two different instances
}
You can use identical(this, other).
For completeness, this is a supplemental answer to the existing answers.
If some class Foo does not override ==, then the default implementation is to return whether they are the same object. The documentation states:
The default behavior for all Objects is to return true if and only if this object and other are the same object.
That's my way how I compare deep 2 Objects they're not the same:
class Foo{
String uid;
bool isActiv;
Foo(this.uid, this.isActiv){}
Map<String, dynamic> toJson() => _$FooToJson(this);
}
Foo A = Foo("alpha", true);
Foo B = Foo("alpha", true);
print(A.toJson().toString() == B.toJson().toString()); // true
B.uid = "beta";
print(A.toJson().toString() == B.toJson().toString()); // false
On a different yet similar note, in cases where the framework calls to check the equality among the objects e.g. in case of list.toSet() to get the unique elements from a list, identical(this, other) may not be a choice. That time the class must override the == operator and the hasCode() methods.
However for this case another way could be to use the equatable package. This saves a lot of boiler plate code and is especially handy when you have lot of model classes.
You can use Equatable library
class Foo extends EquatableMixin{
int? a;
int? b;
SomeClass? c;
SomeOtherClass? d;
Foo(this.a,this.b,this.c,this.d);
// this does the job, it overrides the hashcode and equals operator
// give all properties to this `props`
#override
List<Object> get props => [a,b,c,d];
}
class SomeOtherClass with EquatableMixin{
String name;
SomeOtherClass(this.name);
#override
List<Object> get props => [name];
}
class SomeClass with EquatableMixin{
String name;
SomeClass(this.name);
#override
List<Object> get props => [name];
}
Foo foo =
Foo(1,2,SomeOtherClass("roger"),SomeOtherClassObject("mack"));
Foo foo2 =
Foo(1,2,SomeOtherClass("roger"),SomeOtherClassObject("mack"));
print(foo == foo2) // prints true
So, we don't need to manually override == and hashcode() methods
the library will do that.
Note : the inner objects (SomeClass and SomeOtherClass) should also use EquatableMixin, we can extends this or use as a mixin too

Is there a way to pass a primitive parameter by reference in Dart?

I would like to pass a primitive (int, bool, ...) by reference. I found a discussion about it (paragraph "Passing value types by reference") here: value types in Dart, but I still wonder if there is a way to do it in Dart (except using an object wrapper) ? Any development ?
The Dart language does not support this and I doubt it ever will, but the future will tell.
Primitives will be passed by value, and as already mentioned here, the only way to 'pass primitives by reference' is by wrapping them like:
class PrimitiveWrapper {
var value;
PrimitiveWrapper(this.value);
}
void alter(PrimitiveWrapper data) {
data.value++;
}
main() {
var data = new PrimitiveWrapper(5);
print(data.value); // 5
alter(data);
print(data.value); // 6
}
If you don't want to do that, then you need to find another way around your problem.
One case where I see people needing to pass by reference is that they have some sort of value they want to pass to functions in a class:
class Foo {
void doFoo() {
var i = 0;
...
doBar(i); // We want to alter i in doBar().
...
i++;
}
void doBar(i) {
i++;
}
}
In this case you could just make i a class member instead.
No, wrappers are the only way.
They are passed by reference. It just doesn't matter because the "primitive" types don't have methods to change their internal value.
Correct me if I'm wrong, but maybe you are misunderstanding what "passing by reference" means? I'm assuming you want to do something like param1 = 10 and want this value to still be 10 when you return from your method. But references aren't pointers. When you assign the parameter a new value (with = operator), this change won't be reflected in the calling method. This is still true with non-primitive types (classes).
Example:
class Test {
int val;
Test(this.val);
}
void main() {
Test t = new Test(1);
fn1(t);
print(t.val); // 2
fn2(t);
print(t.val); // still 2, because "t" has been assigned a new instance in fn2()
}
void fn1(Test t) {
print(t.val); // 1
t.val = 2;
}
void fn2(Test t) {
t = new Test(10);
print(t.val); // 10
}
EDIT
I tried to make my answer more clear, based on the comments, but somehow I can't seem to phrase it right without causing more confusion. Basically, when someone coming from Java says "parameters are passed by reference", they mean what a C/C++ developer would mean by saying "parameters are passed as pointers".
As dart is compiled into JavaScript, I tried something that works for JS, and guess what!? It worked for dart!
Basically, what you can do is put your value inside an object, and then any changes made on that field value inside that function will change the value outside that function as well.
Code (You can run this on dartpad.dev)
main() {
var a = {"b": false};
print("Before passing: " + a["b"].toString());
trial(a);
print("After passing: " + a["b"].toString());
}
trial(param) {
param["b"] = true;
}
Output
Before passing: false
After passing: true
One of the way to pass the variables by reference by using the values in List. As arrays or lists are Pass by reference by default.
void main() {
List<String> name=['ali' ,'fana'];
updatename(name);
print(name);
}
updatename(List<String> name){
name[0]='gufran';
}
Try this one, This one of the simplest way to pass by reference.
You can use ValueNotifier
And, you can pass it as ValueListenable to classes or methods that needs to know up-to-date value, but should not edit it:
class Owner {
final theValue = ValueNotifier(true);
final user = User(theValue);
...
}
class User {
final ValueListeneble<bool> theValue;
User(this.theValue);
...
}
It provides more functionality than actually needed, but solves the problem.
If ValueNotifier + ValueListenable do not work for you (you want to make sure the client does not listen to every change of the value, or your package is pure Dart package and thus cannot reference Flutter libraries), use a function:
class Owner {
int _value = 0;
int getValue() => _value;
void increase() => _value++;
}
void main() {
final owner = Owner();
int Function() obtainer = owner.getValue;
print(obtainer());
owner.increase();
print(obtainer());
}
Output will be:
0
1
This approach has memory usage related downside: the obtainer will hold the reference to the owner, and this, even if owner is already not referenced, but obtainer is still reachable, owner will be also reachable
and thus will not be garbage collected.
If you do not want the downside, pass the smaller container than the entire owner:
import 'package:flutter/foundation.dart';
class ListenableAsObtainer<T> implements ValueObtainer<T> {
ListenableAsObtainer(this._listenable);
final ValueListenable<T> _listenable;
#override
T get value => _listenable.value;
}
class FunctionAsObtainer<T> implements ValueObtainer<T> {
FunctionAsObtainer(this._function);
final T Function() _function;
#override
T get value => _function();
}
class ValueAsObtainer<T> implements ValueObtainer<T> {
ValueAsObtainer(this.value);
#override
T value;
}
/// Use this interface when the client needs
/// access to the current value, but does not need the value to be listenable,
/// i.e. [ValueListenable] would be too strong requirement.
abstract class ValueObtainer<T> {
T get value;
}
The usage of FunctionAsObtainer will still result in holding the owner from garbage collection, but two other options will not.
Just to make it clear:
void main() {
var list1 = [0,1,2];
var modifiedList1 = addMutable(list1, 3);
var list2 = [0,1,2];
var modifiedList2 = addImmutable(list2, 3);
print(list1);
print(modifiedList1);
print(list2);
print(modifiedList2);
}
List<int> addMutable(List<int> list, int element){
return list..add(element);
}
List<int> addImmutable(List<int> list, int element){
return [...list, element];
}
Output:
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2]
[0, 1, 2, 3]
All variables are passed by value. If a variable contains a primitive (int, bool, etc.), that's it. You got its value. You can do with it whatever you want, it won't affect the source value. If a variable contains an object, what it really contains is a reference to that object.
The reference itself is also passed by value, but the object it references is not passed at all. It just stayed where it was. This means that you can actually make changes to this very object.
Therefore, if you pass a List and if you .add() something to it, you have internally changed it, like it is passed by reference. But if you use the spread operator [...list], you are creating a fresh new copy of it. In most cases that is what you really want to do.
Sounds complicated. Isn't really. Dart is cool.

Resources