Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Posting JSON-formatted data


This is a recipe for a client web app that sends a request to a web server. The request contains the form's data that is posted in the JSON format.

How to do it...

Look at the project post_form for the code.

  1. Our form (refer the next diagram) will post data for a job in IT; we reuse the class Job in the Making toJSON and fromJSON methods in your class recipe from Chapter 4, Object Orientation. We keep the example short and simple, but add two new properties, posted and open. Have a look at the following code:

    class Job {
      String type;
      int salary;
      String company;
      DateTime posted; // date of publication of job
      bool open = true; // is job still vacant ?
      Job(this.type, this.salary, this.company, this.posted);
      // toJSON and fromJSON methods
    }
  2. The model class is made available to the code in post_form.dart using the following code:

    import '../model/job.dart';
  3. We add our own event handler for the submit button using the following code:

    void main() {
      querySelector...