我想使用 post 方法发送数据,当用户选择一个学期时我提供一个可选的(如果用户选择 KHS 则出现 SEMESTER 下拉列表并且我在正文中提供一个空字符串值)但是当我通过按下提交按钮出现如下错误。
我已经通过尝试文章中的各种参考来更改它,但错误仍然发生并出现不同的错误。
我在小部件中的代码
class _PengajuanPageState extends State<PengajuanPage> {
String? selectedValue;
int? selectedSmt;
List<String> listItem = [
'Transkrip Sementara',
'KHS',
];
List<int> khsListItem = [1, 2, 3, 4, 5, 6, 7, 8];
final _formKey = GlobalKey<FormState>();
final _messageController = TextEditingController();
void submitForm() async {
String url = Constant.baseURL;
String token = await UtilSharedPreferences.getToken();
final response = await http.post(
Uri.parse(
'$url/auth/mhs_siakad/pengajuan/pengajuan_cetak_transkrip/store',
),
headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
body: jsonEncode(
{
'jenis_transkrip': selectedValue,
'semester': selectedSmt ?? '',
'message': _messageController,
},
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: primaryColor,
title: Center(
child: Text(
'Pengajuan Transkrip/KHS',
style: bold5,
),
),
actions: [
IconButton(
icon: const Icon(Icons.history),
onPressed: () {
Navigator.pushNamed(
context,
AppRoute.historypengajuanPage,
);
},
)
],
),
body: Padding(
padding: const EdgeInsets.all(16),
child: Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 10,
),
Text(
'Pilih Jenis Transkrip',
style: regular5,
),
const SizedBox(
height: 8,
),
Container(
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(
1,
2,
),
),
],
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: const Text('Pilih Jenis Transkrip'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue!;
selectedSmt = null;
});
},
items: listItem.map((valueItem) {
return DropdownMenuItem<String>(
value: valueItem,
child: Text(
valueItem,
),
);
}).toList(),
),
),
),
),
const SizedBox(
height: 10,
),
Visibility(
visible: selectedValue == 'KHS',
child: Container(
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.2),
spreadRadius: 1,
blurRadius: 9,
offset: const Offset(
1,
2,
),
),
],
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: DropdownButtonHideUnderline(
child: DropdownButton<int>(
hint: const Text('Pilih Semester'),
value: selectedSmt,
onChanged: (newValue) {
setState(() {
selectedSmt = newValue;
});
},
items: khsListItem.map((value) {
return DropdownMenuItem<int>(
value: value,
child: Text(
value.toString(),
),
);
}).toList(),
),
),
),
),
),
RichText(
text: TextSpan(
text: 'Keperluan Cetak ',
style: GoogleFonts.poppins(
textStyle: const TextStyle(
color: Colors.black,
fontSize: 14,
),
),
children: <TextSpan>[
TextSpan(
text: '*',
style: GoogleFonts.poppins(
textStyle: const TextStyle(
color: Colors.red,
fontSize: 16,
),
),
),
],
),
),
const SizedBox(
height: 8,
),
TextFormField(
controller: _messageController,
maxLines: 4,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(
color: Colors.white,
width: 2.0,
),
),
fillColor: Colors.white,
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
borderSide: const BorderSide(
color: Colors.blue,
),
),
filled: true,
hintText: 'Deskripsikan Keperluan',
border: const OutlineInputBorder(
borderSide: BorderSide(
color: Colors.white,
),
),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Keperluan cetak wajib di isi!';
}
return null;
},
),
const SizedBox(
height: 16,
),
Text(
'*Silahkan isi form dan simpan untuk mengajukan cetak transkrip',
style: regular7.copyWith(
color: Colors.red,
fontStyle: FontStyle.italic,
),
),
const SizedBox(
height: 16,
),
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 45,
width: double.infinity,
decoration: BoxDecoration(
color: Colors.green[100],
borderRadius: BorderRadius.circular(8),
),
child: TextButton(
style: TextButton.styleFrom(
foregroundColor: Colors.green,
),
onPressed: () {
if (_formKey.currentState!.validate()) {
submitForm();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: Text(
'Simpan',
style: bold5,
),
),
),
),
),
],
),
),
),
);
}
}
您在正文中传递 TextEditingController 需要传递那个 _messageController.text 的值
body: jsonEncode(
{
'jenis_transkrip': selectedValue,
'semester': selectedSmt ?? '',
'message': _messageController.text,
},
),