Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:flutter/material.dart';
  4. import 'package:http/http.dart' as http;
  5.  
  6. // class Post {
  7. // final String userId;
  8. // final int id;
  9. // final String title;
  10. // final String body;
  11.  
  12. // Post({this.userId, this.id, this.title, this.body});
  13.  
  14. // factory Post.fromJson(Map<String, dynamic> json) {
  15. // return Post(
  16. // userId: json['userId'],
  17. // id: json['id'],
  18. // title: json['title'],
  19. // body: json['body'],
  20. // );
  21. // }
  22.  
  23. // Map toMap() {
  24. // var map = new Map<String, dynamic>();
  25. // map["userId"] = userId;
  26. // map["title"] = title;
  27. // map["body"] = body;
  28.  
  29. // return map;
  30. // }
  31. // }
  32.  
  33. class BodyRequest {
  34. // final String username;
  35. // final String password;
  36. final int statusCode;
  37. final bool isSuccess;
  38. final String message;
  39. final String results;
  40.  
  41. BodyRequest({this.statusCode, this.isSuccess, this.message, this.results});
  42.  
  43. factory BodyRequest.fromJson(Map<String, dynamic> json) {
  44. return BodyRequest(
  45. // username: json['username'],
  46. // password: json['password'],
  47. statusCode: json['statusCode'],
  48. isSuccess: json['isSuccess'],
  49. message: json['message'],
  50. results: json['results']);
  51. }
  52. // Map toMap() {
  53. // var map = new Map<String, dynamic>();
  54. // map["username"] = username;
  55. // map["password"] = password;
  56.  
  57. // return map;
  58. // }
  59. }
  60.  
  61. Future<BodyRequest> createPost(String url, String body) async {
  62. // Map<String, String> headers = {"Content-type": "application/json", "x-api-key": "4e10956ba06d926751038851124acdce"};
  63. Map<String, String> headers = {
  64. 'Content-type': 'application/json',
  65. 'x-api-key': '4e10956ba06d926751038851124acdce',
  66. };
  67. final response = await http.post(url, headers: headers, body: body);
  68.  
  69. if (response.statusCode == 200) {
  70. // If the call to the server was successful, parse the JSON.
  71. print(json.decode(response.body));
  72. return BodyRequest.fromJson(json.decode(response.body));
  73. } else {
  74. // If that call was not successful, throw an error.
  75. Object object = json.decode(response.body);
  76. print(object);
  77. // throw Exception(object);
  78. // ResponseFailled failled =
  79. // ResponseFailled.fromJson(json.decode(response.body));
  80. throw Exception("Error Connectiion");
  81. }
  82. }
  83.  
  84. class MyApp extends StatelessWidget {
  85. final Future<BodyRequest> post;
  86.  
  87. MyApp({Key key, this.post}) : super(key: key);
  88. // static final CREATE_POST_URL = 'https://jsonplaceholder.typicode.com/posts';
  89. static final CREATE_POST_URL =
  90. 'http://apiproperty.esd.co.id/v1/company/login';
  91. TextEditingController titleControler = new TextEditingController();
  92. TextEditingController bodyControler = new TextEditingController();
  93.  
  94. @override
  95. Widget build(BuildContext context) {
  96. // TODO: implement build
  97. return MaterialApp(
  98. title: "WEB SERVICE",
  99. theme: ThemeData(
  100. primaryColor: Colors.deepOrange,
  101. ),
  102. home: Scaffold(
  103. appBar: AppBar(
  104. title: Text('Create Post'),
  105. ),
  106. body: new Container(
  107. margin: const EdgeInsets.only(left: 8.0, right: 8.0),
  108. child: new Column(
  109. children: <Widget>[
  110. new TextField(
  111. controller: titleControler,
  112. decoration: InputDecoration(
  113. hintText: "title....", labelText: 'Post Title'),
  114. ),
  115. new TextField(
  116. controller: bodyControler,
  117. decoration: InputDecoration(
  118. hintText: "body....", labelText: 'Post Body'),
  119. ),
  120. new RaisedButton(
  121. onPressed: () async {
  122. // var map = new Map<String, dynamic>();
  123. // map["username"] = (titleControler.text.isEmpty)
  124. // ? ""
  125. // : titleControler.text;
  126. // map["password"] =
  127. // (bodyControler.text.isEmpty) ? "" : bodyControler.text;
  128. // BodyRequest newPost = new BodyRequest(
  129. // username: ,
  130. // password: );
  131. String body =
  132. '{"username": "${titleControler.text}","password": "${bodyControler.text}"}';
  133. print(body);
  134. BodyRequest p = await createPost(CREATE_POST_URL, body);
  135. },
  136. child: const Text("Create"),
  137. )
  138. ],
  139. ),
  140. )),
  141. );
  142. }
  143. }
  144.  
  145. void main() => runApp(MyApp());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement