Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:http/http.dart' as http;
- // class Post {
- // final String userId;
- // final int id;
- // final String title;
- // final String body;
- // Post({this.userId, this.id, this.title, this.body});
- // factory Post.fromJson(Map<String, dynamic> json) {
- // return Post(
- // userId: json['userId'],
- // id: json['id'],
- // title: json['title'],
- // body: json['body'],
- // );
- // }
- // Map toMap() {
- // var map = new Map<String, dynamic>();
- // map["userId"] = userId;
- // map["title"] = title;
- // map["body"] = body;
- // return map;
- // }
- // }
- class BodyRequest {
- // final String username;
- // final String password;
- final int statusCode;
- final bool isSuccess;
- final String message;
- final String results;
- BodyRequest({this.statusCode, this.isSuccess, this.message, this.results});
- factory BodyRequest.fromJson(Map<String, dynamic> json) {
- return BodyRequest(
- // username: json['username'],
- // password: json['password'],
- statusCode: json['statusCode'],
- isSuccess: json['isSuccess'],
- message: json['message'],
- results: json['results']);
- }
- // Map toMap() {
- // var map = new Map<String, dynamic>();
- // map["username"] = username;
- // map["password"] = password;
- // return map;
- // }
- }
- Future<BodyRequest> createPost(String url, String body) async {
- // Map<String, String> headers = {"Content-type": "application/json", "x-api-key": "4e10956ba06d926751038851124acdce"};
- Map<String, String> headers = {
- 'Content-type': 'application/json',
- 'x-api-key': '4e10956ba06d926751038851124acdce',
- };
- final response = await http.post(url, headers: headers, body: body);
- if (response.statusCode == 200) {
- // If the call to the server was successful, parse the JSON.
- print(json.decode(response.body));
- return BodyRequest.fromJson(json.decode(response.body));
- } else {
- // If that call was not successful, throw an error.
- Object object = json.decode(response.body);
- print(object);
- // throw Exception(object);
- // ResponseFailled failled =
- // ResponseFailled.fromJson(json.decode(response.body));
- throw Exception("Error Connectiion");
- }
- }
- class MyApp extends StatelessWidget {
- final Future<BodyRequest> post;
- MyApp({Key key, this.post}) : super(key: key);
- // static final CREATE_POST_URL = 'https://jsonplaceholder.typicode.com/posts';
- static final CREATE_POST_URL =
- 'http://apiproperty.esd.co.id/v1/company/login';
- TextEditingController titleControler = new TextEditingController();
- TextEditingController bodyControler = new TextEditingController();
- @override
- Widget build(BuildContext context) {
- // TODO: implement build
- return MaterialApp(
- title: "WEB SERVICE",
- theme: ThemeData(
- primaryColor: Colors.deepOrange,
- ),
- home: Scaffold(
- appBar: AppBar(
- title: Text('Create Post'),
- ),
- body: new Container(
- margin: const EdgeInsets.only(left: 8.0, right: 8.0),
- child: new Column(
- children: <Widget>[
- new TextField(
- controller: titleControler,
- decoration: InputDecoration(
- hintText: "title....", labelText: 'Post Title'),
- ),
- new TextField(
- controller: bodyControler,
- decoration: InputDecoration(
- hintText: "body....", labelText: 'Post Body'),
- ),
- new RaisedButton(
- onPressed: () async {
- // var map = new Map<String, dynamic>();
- // map["username"] = (titleControler.text.isEmpty)
- // ? ""
- // : titleControler.text;
- // map["password"] =
- // (bodyControler.text.isEmpty) ? "" : bodyControler.text;
- // BodyRequest newPost = new BodyRequest(
- // username: ,
- // password: );
- String body =
- '{"username": "${titleControler.text}","password": "${bodyControler.text}"}';
- print(body);
- BodyRequest p = await createPost(CREATE_POST_URL, body);
- },
- child: const Text("Create"),
- )
- ],
- ),
- )),
- );
- }
- }
- void main() => runApp(MyApp());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement