samdangerous
samdangerous

Reputation: 73

Flutter: Adding an Image to canvas, error on compile

I am trying at add an image from a byte buffer, to a canvas element:

Uint8List image = bd.buffer.asUint8List(++offset, len);
await ui.decodeImageFromList(image, addIMage);

Where the callback function, addImage() is

  void printImage(data){
    print("Image stuff");
    print(data);
    canvasPainter.addItem(data);
  }

Canvas painter is implements CustomPainter and is shown below:

class CanvasPainter implements CustomPainter{

 var items = new List();
 void addItems(var item){
 items.add(item);}
 @override
 void paint(Canvas canvas, Size size){
 for (var item in items){ 
   canvas.drawImage(item,new Offset(50.0,50.0),new Paint();
 }

 ...
}



class WriteScreen extends StatefulWidget {
  ScreenState state;

  @override
  ScreenState createState() => state = new ScreenState();
}


class ScreenState extends State<WriteScreen> {
  GestureDetector touch;
  CustomPaint canvas;
  CanvasPainter canvasPainter;

  @override
  void initState() {
    super.initState();
    canvasPainter = new CanvasPainter(const Color.fromRGBO(255, 255, 255, 1.0));
  }

  @override
  Widget build(BuildContext context) {
    touch = new GestureDetector(
      onPanStart: panStart
    );
    final mediaQueryData = MediaQuery.of(context);
    canvasPainter.size = mediaQueryData.size;
    canvas = new CustomPaint( painter: canvasPainter);
  }
}

I am receiving a compile time error in my CustomPainter class:

compiler message: lib/canvas_painter.dart:64:28: Error: A value of type '#lib1::Image' can't be assigned to a variable of type 'dart.ui::Image'.
compiler message: Try changing the type of the left hand side, or casting the right hand side to 'dart.ui::Image'.
compiler message:           canvas.drawImage(item, new Offset(50.0, 50.0), strokePaint);
compiler message:       

I am very confused by the error message, as I am importing Image from dart.ui so I am unsure as to why the compiler is throwing this exception.

Any help/suggestions would be much appreciated.

Upvotes: 1

Views: 4663

Answers (1)

Richard Heap
Richard Heap

Reputation: 51741

Add some types in CanvasPainter and update the question with any new error message.

import 'dart:ui' as ui;
import 'package:flutter/material.dart';
....
class CanvasPainter extends CustomPainter {
  List<ui.Image> items = new List<ui.Image>();

  void addItems(ui.Image item) {
    items.add(item);
  }

  @override
  void paint(Canvas canvas, Size size) {
    for (ui.Image item in items) {
      canvas.drawImage(item, new Offset(50.0, 50.0), new Paint());
    }
  }
}

Upvotes: 2

Related Questions