Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
How do I go about fixing this issue cause I have tried all the solutions:
[ERROR:flutter/runtime/dart_vm_initializer.cc(39)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
late String selectedDestination = '';
late List destinationsList = [];
var isLoaded = false;
Future getAllDestinations() async {
String urlDestinations = "http://localhost/api/destinations/";
var response = await http.get(Uri.parse(urlDestinations));
if(response.statusCode == 200){
var jsonData = json.decode(response.body);
setState((){
destinationsList = jsonData;
print(destinationsList);
An the DropDown code is this
type here
DropdownButtonFormField(
decoration: InputDecoration(
prefixIcon: const Icon(
Icons.location_pin,
color: primary,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
label: const Text(
"Destination to *",
style: TextStyle(
fontSize: 16,
color: primary,
validator: (value) => value == null
? 'Field must not be empty'
: null,
isExpanded: true,
value: selectedDestination,
items: destinationsList.map((item) {
return DropdownMenuItem(
value: item['id'].toString(),
child:
Text(Text(item['name']).toString()),
}).toList(),
onChanged: (String? value) {
setState(() {
selectedDestination = value!;
Sample API
"destinations": [
"id": 1,
"name": "Nairobi",
"city": "Nairobi",
"location": null,
"mobile": "0741761232",
"status": 1,
"created_at": "2022-10-24T11:57:51.000000Z",
"updated_at": "2022-10-24T11:57:51.000000Z"
"id": 2,
"name": "Voi",
"city": "Voi",
"location": null,
"mobile": "0741761235",
"status": 1,
"created_at": "2022-10-24T11:58:05.000000Z",
"updated_at": "2022-10-24T11:58:05.000000Z"
I just need to fetch the name. I am expecting to fetch a list of items in the dropdown.
The reason is you are directly assigning a map to a list when you are decoding the json. Idle way to do a json deserialization should be done using Model Classes. Example: https://docs.flutter.dev/cookbook/networking/fetch-data
To solve your problem you can do:
if(response.statusCode == 200){
var jsonData = json.decode(response.body);
setState((){
destinationsList = jsonData["destinations"];
You need to access the key which has list to assign it to the list of destinations. Like this jsonData["destinations"];
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.