如何使用DIO设置颤振POST方法? [英] How to set flutter POST method using DIO?

查看:54
本文介绍了如何使用DIO设置颤振POST方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的下面的代码,我被卡住了,请帮忙.如何使用DIO设置颤振POST方法?

This is my code below, i'm stuck please help. How to set flutter POST method using DIO?

Map<String, dynamic> toJson() {
    return {
        'id': id,
         "name": name,
         "telNumber": telNumber,
         "email": email,
         "age": age
      };
   }

String postToJson(Post data){
      final dyn = data.toJson();
      return json.encode(dyn);
}

Future<http.Response> createPost(Post post) async {
          final response = await http.post(
               "$baseUrl/users",
              headers: {
                  "content-type": "application"
                    },
              body: postToJson(post));
              return response;
            }

此方法适用于http

This method works in http

推荐答案

 BaseOptions options = new BaseOptions(
     baseUrl: $baseUrl,
     connectTimeout: 10000,
     receiveTimeout: 10000,);
final dioClient = Dio(options);
try{
   final response = await dioClient.post("/users", data: FormData.fromMap(
      postToJson(post))
    ),);
   return response;
} catch (e) {
  throw (e);
}

将此代码放入函数中

这篇关于如何使用DIO设置颤振POST方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆