如何在Flutter中将下拉数据发布到服务器

huangapple go评论75阅读模式
英文:

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

如何在Flutter中将下拉数据发布到服务器

> in API

如何在Flutter中将下拉数据发布到服务器

答案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&lt;String, dynamic&gt; json) {
    status = json[&#39;status&#39;];
    code = json[&#39;code&#39;];
    message = json[&#39;message&#39;];
  }

  Map&lt;String, dynamic&gt; toJson() {
    final Map&lt;String, dynamic&gt; data = new Map&lt;String, dynamic&gt;();
    data[&#39;status&#39;] = this.status;
    data[&#39;code&#39;] = this.code;
    data[&#39;message&#39;] = this.message;
    return data;
  }
}

Create>> pengajuan-services.dart ( pengajuanClass need to be quick fix because u need import this class)

import &#39;dart:convert&#39;;
import &#39;package:http/http.dart&#39; as http;


class transcriptValueServices {
	
  static Future&lt;pengajuanClass&gt; AddpengajuanService(
    String jenis_transkrip,
    String semester,
    String keperluan_cetak,
  ) async {
    try {
      final response = await http
          .post(
            Uri.parse(
              &#39;your URL&#39;,
            ),
            headers: {
			// write all your headers
              &#39;Content-Type&#39;: &#39;application/json&#39;,
              &#39;Authorization&#39;: &#39;Bearer ***&#39;, // If u have a bearer write it or delete this.
            },
            body: jsonEncode({
              &#39;jenis_transkrip&#39;: jenis_transkrip,
              &#39;semester&#39;: semester,
              &#39;keperluan_cetak&#39;: keperluan_cetak,
            }),
          )
          .timeout(const Duration(seconds: 30));
      var json = jsonDecode(response.body);

      if (response.statusCode == 200) {
        return pengajuanClass.fromJson(json);
      }
      return pengajuanClass(code: &quot;999&quot;);
    } catch (e) {
      return pengajuanClass(code: &quot;999&quot;);
      
    }
  }
}

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(&quot;selected data&quot;,&quot;selected data&quot;,&quot;selected data&quot;);

I hope it works ^^

huangapple
  • 本文由 发表于 2023年4月11日 11:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定