首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >getter 'bodyBytes‘不是为类型'Future<Response>函数(Uri,{Map<String,String>?headers})定义的。

getter 'bodyBytes‘不是为类型'Future<Response>函数(Uri,{Map<String,String>?headers})定义的。
EN

Stack Overflow用户
提问于 2021-09-29 16:56:24
回答 1查看 701关注 0票数 0

我已经将dat更新为2.12版本,并得到了以下错误:

未为类型'bodyBytes‘定义getter’bodyBytes‘(Uri,{Map?headers})。尝试导入定义“bodyBytes”的库,将名称更正为现有getter的名称,或者定义名为“bodyBytes”的getter或字段。

代码如下所示:

我在下面有两条红线

代码语言:javascript
运行
复制
"bodyBytes": 1

"result.paths.first": 2

pdf.dart:

代码语言:javascript
运行
复制
class PDFApi {
  static Future<File> loadAsset(String path) async {
    final data = await rootBundle.load(path);
    final bytes = data.buffer.asUint8List();

    return _storeFile(path, bytes);
  }

  static Future<File> loadNetwork(String url) async {
    final response = await http.get; Uri.parse(url);
    final bytes = response.bodyBytes;  <-- here: "bodyBytes": 1

    return _storeFile(url, bytes);
  }

  static Future<File?> pickFile() async {
    final result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['pdf'],
    );

    if (result == null) return null;
    return File(result.paths.first);  <-- here: "result.paths.first": 2
  }

  static Future<File?> loadFirebase(String url) async {
    try {
      final refPDF = FirebaseStorage.instance.ref().child(url);
      final bytes = await refPDF.getData();

      return _storeFile(url, bytes!);
    } catch (e) {
      return null;
    }
  }

  static Future<File> _storeFile(String url, List<int> bytes) async {
    final filename = basename(url);
    final dir = await getApplicationDocumentsDirectory();

    final file = File('${dir.path}/$filename');
    await file.writeAsBytes(bytes, flush: true);
    return file;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-09-30 14:42:01

我将final response = await http.get; Uri.parse(url);更改为最终响应=等待http.get(Uri.parse(url));thnx改为pskink,return File(result.paths.first);更改为return File(result.paths.first!);,然后就可以正常工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69380585

复制
相关文章

相似问题

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