So below is the JSON Structure which we are sending it as a request back. We are sending all three values as 3 separate text fields. But still very new to flutter and I am not able to figure out how to send it as a array.
{
"name333": "myvarientName",
"environment": "test1",
"description": "Desc",
"testcases": [
{
"name": "testname",
"desc": "testdesc",
"condition": "testname"
},
{
"name": "testname",
"desc": "testdesc",
"condition": "testname"
},
{
"name": "testname",
"desc": "testdesc",
"condition": "testname"
}
]
}
Below is the code for sending the data (making api call)
ElevatedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
String variantName = variantNameController.text;
String environment = environmentController.text;
String varianDescription = variantDescriptionController.text;
String testCaseName = testCaseNameController.text;
String testCaseDescription = testCaseDescriptionController.text;
String testCaseCondition = testCaseConditionController.text;
Future<http.Response> createVaraint(Variant post) async {
final response = await http.post(Uri.parse('Api Call URL'),
headers: {HttpHeaders.contentTypeHeader: 'application/json'},
body: variantToJson(post));
return response;
}
Variant variant = Variant(
name333: variantName,
environment: environment,
description: varianDescription,
);
createVaraint(variant).then((value) {
if (value.statusCode > 200) {
print(value.body);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Variant has been created!',
style: TextStyle(fontSize: 18),
),
behavior: SnackBarBehavior.floating,
duration: const Duration(seconds: 4),
elevation: 6.0,
margin: EdgeInsets.all(40.0),
),
);
print('Successssssss!' + value.statusCode.toString());
}
});
}
},
style: ElevatedButton.styleFrom(
primary: Color(0xff0962ff),
textStyle: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(15)),
),
child: Text("Create Variant"),
)
Just to make sure, the testCaseName, testCaseDescription, testCaseCondition are the controllers for the array