我怎样才能使拍子方法扑扑?

时间:2019-08-04 15:43:23

标签: json http flutter

我如何在flutter中为json这样创建post方法

{
  "app_id": "3djdjkdjde",
  "include_external_user_ids": ["88"],
  "contents": {"en": "your order is created"}
}

当您在json中看到它的json时,我的内容出现了问题,它的值为json

我使用post方法创建了此模型,但我不知道如何解析内容,您可以看到我现在将其设置为null

如果此时带有静态消息的内容不需要动态值也可以

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

Notifications notificationsFromJson(String str) => Notifications.fromJson(json.decode(str));

String notificationsToJson(Notifications data) => json.encode(data.toJson());

class Notifications {
    String appId;
    List<String> includeExternalUserIds;
    Contents contents;

    Notifications({
        this.appId,
        this.includeExternalUserIds,
        this.contents,
    });

    factory Notifications.fromJson(Map<String, dynamic> json) => new Notifications(
        appId: json["app_id"],
        includeExternalUserIds: new List<String>.from(json["include_external_user_ids"].map((x) => x)),
        contents: Contents.fromJson(json["contents"]),
    );

    Map<String, dynamic> toJson() => {
        "app_id": appId,
        "include_external_user_ids": new List<dynamic>.from(includeExternalUserIds.map((x) => x)),
        "contents": contents.toJson(),
    };

        static Future<bool> postNotification(serviceUri, notificationData) async {

      var client = new http.Client();
      bool result = false;
      try { 
            final Map<String, String> _notificationData = {
              'app_id': notificationData['app_id'].toString(),
              'include_external_user_ids': orderData['include_external_user_ids'].toString(),
              "contents": null,

            };
            await client.post(serviceUri,
                        body: json.encode(_notificationData), 
                        headers: { 'Authorization' : 'Bearer ' + notificationData['Token'],  "Content-Type": "application/json"}
                      )
                      .then((http.Response response) {

                          var responseModel = json.decode(response.body);
                          if(responseModel != null && responseModel['status'] == true) {
                              result = true;
                          } else {
                              result = false;
                          }
                      });
      }
      catch(e) {
          result = false;
      }
      finally {
        client.close();
      }
      return result;
  }
}

class Contents {
    String en;

    Contents({
        this.en,
    });

    factory Contents.fromJson(Map<String, dynamic> json) => new Contents(
        en: json["en"],
    );

    Map<String, dynamic> toJson() => {
        "en": en,
    };
}

谢谢

1 个答案:

答案 0 :(得分:0)

globalAveragepooling

}

  

在pubspec.yaml文件中添加此http:^ 0.12.0 + 1