英文:
How to post data dropdown to server in flutter
问题
I have a case where I want to send data to the server. here I have 3 bodies that must be sent, but I'm confused.
here the transcript_value key has 2 values, if number 1 is selected then it will send VALUE TRANSCRIPTS without sending SEMESTER and if number 2 is selected then it will send KHS and also SEMESTER (dropdown).
how to POST data to the server with two conditions as below
I'm confused about how to explain it but I hope you understand
Next I show the API and also the display on mobile
英文:
I have a case where I want to send data to the server. here I have 3 bodies that must be sent, but I'm confused.
here the transcript_value key has 2 values, if number 1 is selected then it will send VALUE TRANSCRIPTS without sending SEMESTER and if number 2 is selected then it will send KHS and also SEMESTER (dropdown).
how to POST data to the server with two conditions as below
I'm confused about how to explain it but I hope you understand
Next I show the API and also the display on mobile
> in API
答案1
得分: 1
以下是代码的翻译部分:
// pengajuan-class.dart
class pengajuanClass {
String? status;
String? code;
String? message;
pengajuanClass({this.status, this.code, this.message});
pengajuanClass.fromJson(Map<String, dynamic> json) {
status = json['status'];
code = json['code'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
data['code'] = this.code;
data['message'] = this.message;
return data;
}
}
// pengajuan-services.dart
import 'dart:convert';
import 'package:http/http.dart' as http;
class transcriptValueServices {
static Future<pengajuanClass> AddpengajuanService(
String jenis_transkrip,
String semester,
String keperluan_cetak,
) async {
try {
final response = await http.post(
Uri.parse('your URL'),
headers: {
// 写入所有的头信息
'Content-Type': 'application/json',
'Authorization': 'Bearer ***', // 如果你有Bearer令牌,请写入,或删除这部分。
},
body: jsonEncode({
'jenis_transkrip': jenis_transkrip,
'semester': semester,
'keperluan_cetak': keperluan_cetak,
}),
).timeout(const Duration(seconds: 30));
var json = jsonDecode(response.body);
if (response.statusCode == 200) {
return pengajuanClass.fromJson(json);
}
return pengajuanClass(code: "999");
} catch (e) {
return pengajuanClass(code: "999");
}
}
}
在点击事件中可以像这样调用(不要忘记在点击事件上加上async
,示例:onPressed: () async { }
):
pengajuanClass returnData = await transcriptValueServices.AddpengajuanService("selected data","selected data","selected data");
希望这有所帮助!
英文:
Create>> pengajuan-class.dart ( u can also create all your json to dart >>https://javiercbk.github.io/json_to_dart/ )
class pengajuanClass {
String? status;
String? code;
String? message;
pengajuanClass({this.status, this.code, this.message});
pengajuanClass.fromJson(Map<String, dynamic> json) {
status = json['status'];
code = json['code'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['status'] = this.status;
data['code'] = this.code;
data['message'] = this.message;
return data;
}
}
Create>> pengajuan-services.dart ( pengajuanClass need to be quick fix because u need import this class)
import 'dart:convert';
import 'package:http/http.dart' as http;
class transcriptValueServices {
static Future<pengajuanClass> AddpengajuanService(
String jenis_transkrip,
String semester,
String keperluan_cetak,
) async {
try {
final response = await http
.post(
Uri.parse(
'your URL',
),
headers: {
// write all your headers
'Content-Type': 'application/json',
'Authorization': 'Bearer ***', // If u have a bearer write it or delete this.
},
body: jsonEncode({
'jenis_transkrip': jenis_transkrip,
'semester': semester,
'keperluan_cetak': keperluan_cetak,
}),
)
.timeout(const Duration(seconds: 30));
var json = jsonDecode(response.body);
if (response.statusCode == 200) {
return pengajuanClass.fromJson(json);
}
return pengajuanClass(code: "999");
} catch (e) {
return pengajuanClass(code: "999");
}
}
}
U can call like this in click event >> ( dont forget to write async to click event example: onPressed: () async { } )
//"pengajuanClass" and "transcriptValueServices" need to be quick fix because u need import
pengajuanClass returnData = await transcriptValueServices.AddpengajuanService("selected data","selected data","selected data");
I hope it works ^^
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论