Mike Osborn
Mike Osborn

Reputation: 503

The getter 'bodyBytes' isn't defined for the type 'Future<Response> Function(Uri, {Map<String, String>? headers})'

I have updated dat to version 2.12 and I am getting this error:

The getter 'bodyBytes' isn't defined for the type 'Future Function(Uri, {Map<String, String>? headers})'. Try importing the library that defines 'bodyBytes', correcting the name to the name of an existing getter, or defining a getter or field named 'bodyBytes'.

By code is like the following below:

I am getting 2 red lines below as

"bodyBytes": 1

"result.paths.first": 2

Code

pdf.dart:

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;
  }
}

Upvotes: 0

Views: 1349

Answers (1)

Mike Osborn
Mike Osborn

Reputation: 503

I changed final response = await http.get; Uri.parse(url); to final response = await http.get(Uri.parse(url)); thnx to pskink and return File(result.paths.first); to return File(result.paths.first!); , then it works just fine.

Upvotes: 0

Related Questions