I've spent around an hour looking for this solution somewhere online. I'm new to flutter & dart languages but I'm very comfortable with C# and .net. Even tho dart/flutter use C# syntax a lot of the language feels much different than I thought it would.
I have a restful API in .net which returns a json object of String : String and String : [Array of Strings]. I have an object class within flutter where I can deserialize the response. I already done this with a normal response of just List and String without a problem but now I ran into massive problem. I don't have a clue how I can deserialise a Json that looks like this.
As requested
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
centerTitle: true,
backgroundColor: Colors.blueGrey,
foregroundColor: Colors.white,
),
drawer: const NavigationDrawer(),
body: Column(
children: [
Center(
child: Text(templateName),
),
Center(
child: FutureBuilder<TemplateContentAndArgumentsObject>(
future: templateContent,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data?.TemplateContent ?? "null");
} else {
return CircularProgressIndicator();
}
},
),
),
],
),
);
Api Call Code
Future<TemplateContentAndArgumentsObject> getTemplateContent(
String customerId, String templateName) async {
var url = Uri.parse(
'https://localhost:7167/api/v1/Templates/$customerId/$templateName');
var response = await http.get(url, headers: {
"Accept": "application/json",
"Access-Control-Allow-Origin": "*"
});
try {
print(response.body);
var sm = json.decode(response.body);
print(sm);
} catch (ex) {
print(ex);
}
if (response.statusCode == 200) {
TemplateContentAndArgumentsObject obj =
TemplateContentAndArgumentsObject.fromJson(json.decode(response.body));
print(obj.TemplateContent);
print(obj.TemplateArguments);
return obj;
} else {
print('Request failed with status: ${response.statusCode}');
}
return TemplateContentAndArgumentsObject(
TemplateContent: "", TemplateArguments: new List<String>.empty());
}
Class Object
import 'package:flutter/cupertino.dart';
class TemplateContentAndArgumentsObject {
String TemplateContent;
List<String> TemplateArguments;
TemplateContentAndArgumentsObject({
required this.TemplateContent,
required this.TemplateArguments,
});
factory TemplateContentAndArgumentsObject.fromJson(
Map<String, dynamic> json,
) =>
TemplateContentAndArgumentsObject(
TemplateContent: json["TemplateContent"] as String,
TemplateArguments: (json["TemplateArguments"] as List<String>),
);
}