首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >参数类型'Future<Uint8List?>‘不能分配给参数类型'Future<Uint8List>’?

参数类型'Future<Uint8List?>‘不能分配给参数类型'Future<Uint8List>’?
EN

Stack Overflow用户
提问于 2022-09-11 04:53:43
回答 1查看 797关注 0票数 -1

我试图从url中读取pdf,音频和打印包,但奇怪的是,我得到的不是一个,而是下面提到的三个错误。我还附上了图像,以清楚地看到我是在哪里得到的错误。

代码语言:javascript
运行
复制
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>'.

代码语言:javascript
运行
复制
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;
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-11 06:30:34

尝试这段代码

代码语言:javascript
运行
复制
 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);
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73676921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档