jonataslaw / get_server

A backend server that makes it possible to program with Flutter syntax and reuse existing code
Apache License 2.0
476 stars 42 forks source link

upload file and got cast error #68

Open tang85 opened 3 years ago

tang85 commented 3 years ago

Upload file to server and got cast error in file widget.dart @jonataslaw

Error is _CastError (type 'Future<Map<dynamic, dynamic>?>' is not a subtype of type 'FutureOr<Map<dynamic, dynamic>>' in type cast)

Function is

@override
  Future<MultipartUpload> file(String name, {Encoding encoder = utf8}) async {
    final payload = await (request.payload(encoder: encoder)
        as FutureOr<Map<dynamic, dynamic>>);
    final multiPart = await payload[name];
    if (multiPart is MultipartUpload) {
      return multiPart;
    } else {
      Get.log('Incorrect format, upload the file as Multipart/formdata',
          isError: true);
      return MultipartUpload(null, null, null, null);
    }
  }

Maybe we should wrap function's body with try..catch as below.

 @override
  Future<MultipartUpload> file(String name, {Encoding encoder = utf8}) async {
    try {
      final payload = await (request.payload(encoder: encoder)
          as FutureOr<Map<dynamic, dynamic>?>);

      final multiPart = await payload![name];
      if (multiPart is MultipartUpload) {
        return multiPart;
      } else {
        Get.log('Incorrect format, upload the file as Multipart/formdata',
            isError: true);
        return MultipartUpload(null, null, null, null);
      }
    } catch (e) {
      return MultipartUpload(null, null, null, null);
    }
  }

// I love get_server,It's Great!

kader46 commented 2 years ago

Hello @tang85 ! did you find any solution ?