我有一个必须使用PUT方法但不起作用的API.我试了太多方法,但都不管用. 嗯,我不知道为什么PUT方法不起作用,或者可能是我的错.然而,postman 的推杆方法是正确的,我不能在Flutter 中修复它.


下面是我的Send-Put方法代码

My parameters used on the put request

{
    "phone_number": "155556485515", // required
    "full_name": "AMIN AZIZZADEH", // required
    "admin_role": "1", // required| 1 => admin | 2 => operator
    "password": "#edg$#sdghd", // required
    "password_confirmation": "#edg$#sdghd", // required
    "email": "amin@gmail.com", // nullable
    "is_active": "0", // 0 => active | 1 => deactive
    "national_id": "125852", // nullable | shomare shenasname
    "address": "US" // nullable
}

Send method :

这只是我的代码中不发送PUT方法的Send部分.

    Map<String, String> authorization = {};
    Map<String, dynamic> bodyMap = {};

    var uri =
        Uri.parse(config.url);
    var response = await http.put(
      uri,
      headers: {
        HttpHeaders.authorizationHeader:'Bearer ${config.token}',
        HttpHeaders.acceptHeader: 'application/json',
      },
      body: bodyMap,
    );
    print('response boz -> ${response.body}');

日志(log)

enter image description here

我试过这种方法,但我做不到.

var response = await put(
  config.url,
  headers: authorization,
  jsonEncode(
    <String, String>{
"phone_number": "155556485515", 
"full_name": "AMIN AZIZZADEH", 
"admin_role": "1", 
"password": "#edg$#sdghd",
"password_confirmation": "#edg$#sdghd",
"email": "amin@gmail.com",
"is_active": "0",
"national_id": "125852",
"address": "US",
    },
  ),
);

推荐答案

你应该将你的响应从json解码到Map,你可以使用jsonDecode来解码你的响应,看下面的例子,

import 'dart:convert';

void main() {
  String jsonString = '{"name": "Amin", "age": 25}';
  Map<String, dynamic> decodedMap = jsonDecode(jsonString);

  print(decodedMap['name']); // Output: Amin
  print(decodedMap['age']);  // Output: 25
}

这是如何使用jsonDecode的示例.

当我们try 使用您的代码时,我们可以获得以下更新,

var response = await http.put(
  uri,
  headers: {
    HttpHeaders.authorizationHeader: 'Bearer ${config.token}',
    HttpHeaders.acceptHeader: 'application/json',
    HttpHeaders.contentTypeHeader: 'application/json',
  },
  body: jsonEncode(bodyMap), // Encode bodyMap to JSON format
);

因此,据我所知,您的请求发送部分是可以的,

var response = await put(
  config.url,
  headers: authorization,
  jsonEncode(
    <String, String>{
"phone_number": "155556485515", 
"full_name": "AMIN AZIZZADEH", 
"admin_role": "1", 
"password": "#edg$#sdghd",
"password_confirmation": "#edg$#sdghd",
"email": "amin@gmail.com",
"is_active": "0",
"national_id": "125852",
"address": "US",
    },
  ),
);

Flutter相关问答推荐

如何在应用栏中将列置于中心

在Riverpod ConsumerWidget中使用文本编辑控制器

我如何才能动态地将小部件排成一排进行排列呢?

将图标呈现在Flutter 克牌小工具的角落上

blocTest错误:没有从`when()`中调用方法存根

我不能在Fighting中裁剪图片输入错误,否则应用程序会崩溃

如何在抖动中将PNG图像转换为WebP

不要跨同步间隔使用BuildContext

从future 列表到流列表Flutter Firebase

dotenv:未处理的异常:FileNotFoundError的实例

Flutter中不使用Container是否可以做margin

如何在Flutter 中使用选定的键和值从 map 列表创建 map

有没有办法在 google_map 上移动标记(向左或向右移动一些像素)?

Riverpod 2.3.6:AutoDisposeNotifier和ref.onDispose自动处理

你如何在 Flutter 中组合两个数组?

Cloud firestore:如何使用 map 对象创建和更新嵌套数组

如何判断 text.contains() 是否包含多个值

如何从dart 中的当前日期时间中减go 1 天?

如何从 Firebase Firestore 中获取具有特定字段值的文档?

Flutter Firebase 可以在真实设备上运行,而不是在 Android 模拟器上运行