I have an async function that gets some data from SQL server. The error points to the last line of this function. I am using an await
when I call this function in another file. Why am I getting this error? I need to end up with a List<Map<String, String>>
to use in another file. I already tried doing res = json.decode(res) as List<Map<String, String>>;
but this didn’t work.
Future<List<Map<String, String>>> getServerList() async {
//...
//res is a String here
//example of data inside res variable: [{ServerName: Server1, ServerIP: IP1, DatabaseName: DB1, PortNum: 1433, Status: null}, {ServerName: Server2, ServerIP: IP2, DatabaseName: DB2, PortNum: 1433, Status: null}]
var res = await SqlConn.readData("declare @Status nvarchar(4000) exec spHandheldServerList @Status output");
//...
//Changes res from a String to a List<dynamic>
res = json.decode(res);
return res; //ERROR POINTS HERE
}
2
Answers
Remove the ‘Future’ from
Future<List<Map<String, String>>> getServerList() async
to return the list you need of typeList<Map<String, String>>
.Basically your variable stops being of type ‘Future’ when you resolve it with the await and convert it to json (which already returns the list of type
List<Map<String, String>>
)When
json.decode
parses JSON, the runtime type of the returned value is going to be one of the following depending on the JSON string that was parsed:null
bool
int
double
String
List<dynamic>
Map<String, dynamic>
(In the last two cases, every item in the list or map will also be one of these types.)
In your case, it is
List<dynamic>
, which is not implicitly convertible to any other kind of list. You need to call eithercast
ormap
(or both) in order to explicitly convert the list to the type you need. And because you’re dealing withMap
s that’s nested within aList
, not only do you need to convert the outerList
, you also need to convert the innerMap
s.If the structure of the JSON is known beforehand, you want to be parsing any
Map
you have into a data class. This gives you type safety and reliability that are impossible to have from maps alone.