Converting object to an encodable object failed: Instance of '_CompactLinkedHashSet<List<String>>'

When I try to call the function below, flutter return this error: Converting object to an encodable object failed: Instance of '_CompactLinkedHashSet<List>'

Code:

 Future<void> signupVisitor() async {
    final _baseUrlToinu = 'https://api.toinu.com.br/api/v1/auth/signup';

    final response = await http.post(_baseUrlToinu,
        body: json.encode({
          "nameCountry": "Brazil",
          "states": [
            {
              "initials": "MG",
              "cities": {
                ["city 1", "city 2", "city 3"]
              }
            }
          ]
        }));

    final responseBody = json.decode(response.body);
    print(responseBody);

      }

Upvotes: 0

Views: 1323

Answers (1)

KuKu
KuKu

Reputation: 7492

Try to fix 'cities' value.
I think this {['city1', 'citi2'...]} code is wrong.
The 'cities' value is a object because you use '{}' but there is no key.
If you want 'cities' value as array [], you need to erase '{}'.

json.encode({
          "nameCountry": "Brazil",
          "states": [
            {
              "initials": "MG",
              "cities": 
                ["city 1", "city 2", "city 3"]
              
            }
          ]
        })

Upvotes: 1

Related Questions