getx icon indicating copy to clipboard operation
getx copied to clipboard

GetConnect stream has already been listened to on multipart use

Open Ashkan4472 opened this issue 3 years ago • 7 comments

When using multipart for file upload, the "stream has already been listened to" will be thrown

example:

class ModelProvider extends HttpProvider {

  @override
  void onInit() {
    super.onInit();
  }

  Future<Response<MyModel>> addImageToModel({
    Uint8List? image,
  }) {
    final formData = FormData({
      "image": image == null
          ? null
          : MultipartFile(image, filename: 'image'),
      "image2": image == null
          ? null
          : MultipartFile(image, filename: 'image2'),
    });

    return post('myRoute', formData);
  }
}

and this is HttpProvider:

import 'package:get/get.dart';

const API_ENDPOINT = 'http://10.0.2.2:3000/';

class HttpProvider extends GetConnect {
  static String? token = "";
  SecureStorageController secureStorage = Get.find();

  @override
  void onInit() {
    httpClient.baseUrl = API_ENDPOINT;
    httpClient.maxAuthRetries = 2;

    httpClient.addAuthenticator<dynamic>((request) async {
      final token = await this.secureStorage.getAuthToken();
      HttpProvider.token = token;
      if (token != null && token != "") {
        request.headers['Authorization'] = 'Bearer $token';
      }
      return request;
    });

    super.onInit();
  }
}

Expected behavior Upload files without errors

Flutter Version: 2.2.3

Getx Version: ^4.3.7

Ashkan4472 avatar Aug 13 '21 00:08 Ashkan4472