Home > Blockchain >  Uncaught (in promise) Error: Expected a value of type 'String', but got one of type '
Uncaught (in promise) Error: Expected a value of type 'String', but got one of type '

Time:02-03

My code:

          var postdata = {
            "id": "6DFA5E06-3B1C-4507-B831-2D84B4137011", 
            "subject": "Test", 
            "message": "Test", 
            "recipients": ["6CA682DD-BC13-40FB-BB31-1971553DE8F6", "763661B9-0378-4F81-8A98-6B5962752D4A"]
          },
          res = await http.post(
              Uri.parse(AuthService().baseUrl   uri),
              body: postdata, 
              headers: {
                "Accept": "application/json",
                "Content-Type": "application/x-www-form-urlencoded",
                'Authorization': 'Bearer '   value["token"],
              },
          );

Got this error: Uncaught (in promise) Error: Expected a value of type 'String', but got one of type 'List'

CodePudding user response:

You should encode your data using dart built-in functions for JSON in the dart:convert package. Dart uses Map<String, dynamic> in place of JSON.

Import dart:convert into your program and try this:

    var postdata = {
        "id": "6DFA5E06-3B1C-4507-B831-2D84B4137011", 
        "subject": "Test", 
        "message": "Test", 
        "recipients": ["6CA682DD-BC13-40FB-BB31-1971553DE8F6", "763661B9-0378-4F81-8A98-6B5962752D4A"]
    },
    res = await http.post(
        Uri.parse(AuthService().baseUrl   uri),
        body: jsonEncode(postdata), 
        headers: {
          "Accept": "application/json",
          "Content-Type": "application/x-www-form-urlencoded",
          'Authorization': 'Bearer '   value["token"],
        },
    );

Here is the link to the Flutter Cookbook explaining this:

Flutter - Send data to the internet

  •  Tags:  
  • Related