0

I'm trying to upload images and some data via API from my app and I have no error in my console and I don't know what's wrong with my function. This is the code which I use:

upload(File imageFile) async {
    var user =
        Provider.of<LoginUserProvider>(context, listen: false).userData.data;
    DateFormat formater = DateFormat('yyyy-MM-dd');
    String formatted = formater.format(dateTime);
    var stream =
        // ignore: deprecated_member_use
        new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    var length = await imageFile.length();

    var headers =
        Provider.of<LoginUserProvider>(context, listen: false).httpHeader;

    var uri = Uri.parse(
        "xyz");

    var request = new http.MultipartRequest("POST", uri);
    request.headers.addAll(headers);
    //request.headers.addAll(Environment.requestHeaderMedia);
    var multipartFile = new http.MultipartFile(
      'attachment',
      stream,
      length,
      filename: imageFile.path,
      contentType: MediaType('application', 'x-tar'),
    );
    request.fields['section_id'] = VillaID.toString();
    request.fields['date'] = formatted;
    request.fields['description'] = descriptionController.text;

    request.files.add(multipartFile);
    var response = await request.send();
    print(response.statusCode);
    response.stream.transform(utf8.decoder).listen((value) {
      print(value);
    });

    try {
      final streamedResponse = await request.send();
      final response = await http.Response.fromStream(streamedResponse);
      print(json.decode(response.body));
      final responseData = json.decode(response.body) as Map<String, dynamic>;

      if (response.statusCode == 200 || response.statusCode == 201) {
        return true;
      }catch (error) {
      print(error);
      return false;
    }
    return true;
  }

So can anyone help me with my issue, please!

Akif
  • 7,098
  • 7
  • 27
  • 53
Mariam Younes
  • 389
  • 6
  • 29
  • remove try-catch to view the error in the console – MRazaImtiaz Dec 07 '20 at 11:01
  • can you refer which part should i remove – Mariam Younes Dec 07 '20 at 11:06
  • don't remove any part of code, remove only `try{}` `catch(){}`, so the error doesn't catch by the try-catch block. – MRazaImtiaz Dec 07 '20 at 11:10
  • i removed it but i have error the response and response.body is are not defined – Mariam Younes Dec 07 '20 at 11:12
  • the mentioned error is in the console? can you pls share the console message with us – MRazaImtiaz Dec 07 '20 at 11:19
  • D/libc-netbsd(19944): getaddrinfo: readyworx.com get result from proxy >> I/flutter (19944): 200 I/flutter (19944): {"data":null,"status":1,"massage":"Your Request Is Done"} E/flutter (19944): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Bad state: Can't finalize a finalized Request. – Mariam Younes Dec 07 '20 at 11:25
  • you can find the [cause here](https://stackoverflow.com/questions/51096991/dart-http-bad-state-cant-finalize-a-finalized-request-when-retrying-a-http) it's related to sending the same request twice. – MRazaImtiaz Dec 07 '20 at 11:29
  • i showed that from 30 min ago but i don't where is the same request twice – Mariam Younes Dec 07 '20 at 11:31

1 Answers1

0

you are sending the same request twice

1st

var response = await request.send();

Second

final streamedResponse = await request.send();

before sending the same request, create them again.

regarding your code you don't need to create a response again. use the first one in the other places.

MRazaImtiaz
  • 1,964
  • 1
  • 13
  • 23
  • when i do that new error is appear {status: 0, message: Un Authenticated} E/flutter (19944): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. E/flutter (19944): Receiver: null E/flutter (19944): Tried calling: []("section_id") – Mariam Younes Dec 07 '20 at 11:47
  • please edit your question explain the new error & add the console message with formatting and don't forget to put some piece of code related to the error. – MRazaImtiaz Dec 07 '20 at 12:00