skip to Main Content

I am trying to read the pdf from url using pdf, dio and printing packages but the weird thing is I am getting not one but three errors mentioned below. I have also attached the image for clear view where I am getting the errors.

The argument type 'Future<Uint8List?>' can't be assigned to the parameter type 'Future<Uint8List>?
The return type 'Uint8List?' isn't a 'FutureOr<Uint8List>', as required by the closure's context.
The argument type 'List<int>?' can't be assigned to the parameter type 'List<int>'.

enter image description here

class _MyHomePageState extends State<MyHomePage> {
  String _url =
      "https://www.au-sonpo.co.jp/corporate/upload/article/89/article_89_1.pdf";

  void _refresh() {
    setState(() {
      // other _url
      _url =
      "https://www.au-sonpo.co.jp/corporate/upload/article/90/article_90_1.pdf";
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FutureBuilder<Uint8List>(
        future:  _fetchPdfContent(_url),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return PdfPreview(
              allowPrinting: false,
              allowSharing: false,
              canChangePageFormat: false,
              initialPageFormat:
              PdfPageFormat(100 * PdfPageFormat.mm, 120 * PdfPageFormat.mm),
              build: (format)  =>  snapshot.data,
            );
          }
          return Center(
            child: CircularProgressIndicator(),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _refresh,
        tooltip: 'Refesh',
        child: Icon(Icons.refresh),
      ),
    );
  }

  Future<Uint8List?> _fetchPdfContent(final String url) async {
    try {
      final Response<List<int>> response = await Dio().get<List<int>>(
        url,
        options: Options(responseType: ResponseType.bytes),
      );
      return Uint8List.fromList(response.data);
    } catch (e) {
      print(e);
      return null;
    }
  }
}

2

Answers


  1. In your FutureBuilder, you expect Uint8List but in your future you are return nullable type, Try this:

    class _MyHomePageState extends State<MyHomePage> {
      String _url =
          "https://www.au-sonpo.co.jp/corporate/upload/article/89/article_89_1.pdf";
    
      void _refresh() {
        setState(() {
          // other _url
          _url =
              "https://www.au-sonpo.co.jp/corporate/upload/article/90/article_90_1.pdf";
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: FutureBuilder<Uint8List?>(
            future: _fetchPdfContent(_url),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return PdfPreview(
                  allowPrinting: false,
                  allowSharing: false,
                  canChangePageFormat: false,
                  initialPageFormat:
                      PdfPageFormat(100 * PdfPageFormat.mm, 120 * PdfPageFormat.mm),
                  build: (format) => snapshot.data,
                );
              }
              return Center(
                child: CircularProgressIndicator(),
              );
            },
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _refresh,
            tooltip: 'Refesh',
            child: Icon(Icons.refresh),
          ),
        );
      }
    
      Future<Uint8List?>? _fetchPdfContent(final String url) async {
        try {
          final Response<List<int>> response = await Dio().get<List<int>>(
            url,
            options: Options(responseType: ResponseType.bytes),
          );
          if (response.data != null) {
            return Uint8List.fromList(response.data!);
          } else {
            return null;
          }
        } catch (e) {
          print(e);
          return null;
        }
      }
    }
    
    Login or Signup to reply.
  2. try this code

     Future<Uint8List?> _fetchPdfContent(final String url) async {
       var Response<List<int>> response
       try {
        response  = await Dio().get<List<int>>(
           url,
           options: Options(responseType: ResponseType.bytes),
         );
         
       } catch (e) {
         print(e);
         throw "put your error";
       }
    
      return Uint8List.fromList(response.data);
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search