简体   繁体   中英

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);

      }

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"]
              
            }
          ]
        })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM