简体   繁体   English

在flutter中使用ID从API中获取数据

[英]fetch data from an API with the use of ID in flutter

I work on a delivery man application and I need to have from an API the details of an order received on the account of a delivery man.我在一个送货员应用程序上工作,我需要从 API 获得送货员帐户上收到的订单的详细信息。

here is an example:这是一个例子:

{
    "Status": "1",
    "Order": {
        "deliveryCost": "5.000",
        "amount": "38.400",
        "total": "43,400",
        "hors_remise": "34.560",
        "Dishes": [
            {
                "cartID": "00000",
                "dishID": "13548",
                "dimensionID": 0,
                "title": "Classic 16 pieces trays:",
                "ingredient": "10 piece trays + 2 gunkan + 4 shrimp crunchy",
                "price": "38.400",
                "quantity": "1",
                "imagePath": "https://www.monresto.net/test/plats/P_170121112803.jpg",
                "Options": []
            }
        ],

and to access of all this information I must specify the "KEY" I had on the postan为了访问所有这些信息,我必须指定我在邮局上的“KEY”

my question is how can i add (orderID)?我的问题是如何添加(orderID)?

there is my code :有我的代码:

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/material.dart';

class datePageClass extends StatefulWidget {
  @override
  _datePageClassState createState() => _datePageClassState();
}

class _datePageClassState extends State<datePageClass> {

  void getData() async {
    http.Response response = await http.post(
        'https://api/ws/v3/Delivery/orderDetails.php');

    if (respo
nse.statusCode == 200) {
      String data = response.body;

      var cmddetails = jsonDecode(data);
      print(cmddetails);
    } else {
      print(response.statusCode);
    }

    print('');
  }

  @override
  Widget build(BuildContext context) {
    getData();
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.lightBlue,
      ),
    );
  }
}

Did you try this?你试过这个吗?

var response = await http.post('https://www.apiurl.com/ws/v2/details.php',  body: {'OrderID': 1209, 'OtherParam': 'value'});

Then in PHP然后在 PHP

$OrderID = $_POST["OrderID"];
$OtherParam = $_POST["OtherParam"];

Make sure to sanitize the variables to avoid SQL Injections确保清理变量以避免SQL Injections

And then you can send JSON back to dart/flutter.然后你可以将 JSON 发送回 dart/flutter。

Pre-check: Are your sure that this URL expects http-POST not http-GET?预检查:您确定此 URL 需要 http-POST 而不是 http-GET?

You want to add orderID to the request, right?您想将orderID添加到请求中,对吗?
If that's the case, first you need to find if API expects this parameter as query parameter or part of the body.如果是这种情况,首先您需要查找 API 是否希望此参数作为查询参数或主体的一部分。

Nevertheless, here are examples for both cases.尽管如此,以下是这两种情况的示例。

String orderDetailsUrl = 'https://www.monresto.net/ws/v3/Delivery/orderDetails.php';

  1. orderID as query parameter: orderID 作为查询参数:
orderDetailsUrl += "?orderID=FIXME_ORDER_ID";  
http.Response response = await http.post(orderDetailsUrl);

  1. orderID as part of request body: orderID作为请求正文的一部分:
Map<String, dynamic> data = Map()
      ..putIfAbsent("orderID", () => "FIXME_ORDER_ID")
var body = jsonEncode(data);
http.Response response = await http.post(orderDetailsUrl, headers: {"Content-Type": "application/json"}, body: body);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM