Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Error : type '_Map<String, dynamic>' is not a subtype of type 'List<dynamic>'

Tags:

json

flutter

What I want to do is to list the data from the item with DropdownButtonFormField.

I have json result data: (This is also my class, I convert it here: https://javiercbk.github.io/json_to_dart/ )

{
    "status": 200,
    "statusTexts": [
        "Successful"
    ],
    "item": [
        {
            "id": 1,
            "code": "01",
            "definition": "Sayaç"
        }
    ],
    "total": 1,
}

Here is my Json services:


class getListALLServices {
  static Future<List<envanterTipiClass>> EnvanterTipiListGetir() async {
    try {
      final response = await http
          .post(
            Uri.parse("https://"),
            headers: {
              'Content-Type': 'application/json',
              'Authorization':
                  'Bearer ',
            },
            body: jsonEncode({
              'sort': {'column': 'id', 'type': 'asc'},
              "offset": 0,
              "take": 10
            }),
          )
          .timeout(const Duration(seconds: 30));

      if (response.statusCode == 200) {
        final List<dynamic> Json = json.decode(response.body);
        return Json.map((envanterTipiJson) =>
            envanterTipiClass.fromJson(envanterTipiJson)).toList();
      }
      throw Exception('Failed to load');
    } catch (e) {
      print(e);
      throw Exception('Failed to load');
    }
  }
}

My page body:

Center(
                        child: FutureBuilder<List<envanterTipiClass>>(
                          future: _envanterListFuture,
                          builder: (context, snapshot) {
                            if (snapshot.hasData) {
                              return DropdownButtonFormField<envanterTipiClass>(
                                decoration: InputDecoration(
                                  labelText: 'City',
                                  border: OutlineInputBorder(),
                                ),
                                value: selectedEnvanterTip,
                                onChanged: (value) {
                                  setState(() {
                                    selectedEnvanterTip = value;
                                  });
                                },
                                items: snapshot.data!.map((envanterTip) {
                                  return DropdownMenuItem<envanterTipiClass>(
                                    value: envanterTip,
                                    child: Text(
                                        '${envanterTip.item!.first.id.toString()}, ${envanterTip.item!.first.definition.toString()}'),
                                  );
                                }).toList(),
                              );
                            } else if (snapshot.hasError) {
                              return Text('${snapshot.error}');
                            }
                            return const CircularProgressIndicator();
                          },
                        ),
                      ),

But ı'm getting this error: type '_Map<String, dynamic>' is not a subtype of type 'List'

How can fix it? Thanks in advance.^^

like image 607
BurakBK Avatar asked Jun 08 '26 14:06

BurakBK


1 Answers

It goes wrong on

final List<dynamic> Json = json.decode(response.body);

Because the response is not a list, it's a map. "item" is a list, but that's inside the response. Maybe you want to do

final List<dynamic> Json = json.decode(response.body)['item'];

EDIT:
Looking deeper into the code I notice that you actually probably just want to do

  if (response.statusCode == 200) {
    return envanterTipiClass.fromJson(json.decode(response.body));
  }

And for that you need to change the return type from Future<List<envanterTipiClass>> to Future<envanterTipiClass> and change the body from

                        child: FutureBuilder<List<envanterTipiClass>>(

to

                        child: FutureBuilder<envanterTipiClass>(

and

                                items: snapshot.data!.map((envanterTip) {
                                  return DropdownMenuItem<envanterTipiClass>(
                                    value: envanterTip,
                                    child: Text(
                                        '${envanterTip.item!.first.id.toString()}, ${envanterTip.item!.first.definition.toString()}'),
                                  );
                                }).toList(),

to

                                items: snapshot.data!.item!.map((envanterTip) {
                                  return DropdownMenuItem<envanterTipiClass>(
                                    value: envanterTip,
                                    child: Text(
                                        '${envanterTip.id.toString()}, ${envanterTip.definition.toString()}'),
                                  );
                                }).toList(),
like image 186
Ivo Beckers Avatar answered Jun 11 '26 05:06

Ivo Beckers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!