Home > Enterprise >  error in MemoryImage not defined in package pdf/widgets.dart
error in MemoryImage not defined in package pdf/widgets.dart

Time:11-01

import 'dart:io';
import 'package:flutter/material.dart' as material ;
import 'package:flutter/services.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;


class PdfParagraphApi {
  
  static Future<void> generate(key) async {
    final pdf = pw.Document();
    final img = pw.MemoryImage(await rootBundle.load('assets/img.png')).buffer.asUint8List();
    pdf.addPage(
      pw.MultiPage(
        build: (context) => <pw.Widget>[
          pw.Image(img),
       ]),
    );
  }
}

i'm trying to create a pdf with an image inside it and i'm using the library pdf/widgets.dart to create the image as i found in flutters documentation but i'm facing a problem. the error message is the following : The function 'MemoryImage' isn't defined. Try importing the library that defines 'MemoryImage', correcting the name to the name of an existing function, or defining a function named 'MemoryImage'.dartundefined_function

insert image into pdf not working

CodePudding user response:

Replace pw.MemoryImage with material.MemoryImage, because MemoryImage comes from the painting library, which is included inside the material library.

CodePudding user response:

Try to replace the below import:

import 'package:flutter/material.dart' as material ;

with this import 'package:flutter/material.dart';

  • Related