Passing Canvas' 2D Context in Dart
-
02-06-2021 - |
Pregunta
I have a function that accepts CanvasRenderingContext2D as a parameter and does the drawing. Whenever I try to pass it, I get this:
CanvasRenderingContext2D is not assignable to CanvasRenderingContext2D
That function looks like this:
void draw(CanvasRenderingContext2D context) {
...
}
I thought everything is passed as a reference in Dart (just like JS), which shouldn't cause such problems. Is there a way to say I want a reference to the object and not the object it self? Or is there something else I should know?
EDIT:
The problem was that in one file I imported dart:html and in another I had dart:dom. The names for corresponding interfaces are the same but they are different.
Solución
This is likely because you are using dart:dom and dart:html in the same application. To avoid such errors you should import one of them with a namespace
#import('dart:html');
#import('dart:dom', prefix: 'dom');
then you can access code defined in both of them as
window // dart:html window
dom.window // dart:dom window
for more information see this answer