Coder Social home page Coder Social logo

jeffstahlin / parsingjson-flutter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from poojab26/parsingjson-flutter

0.0 1.0 0.0 123 KB

Experimenting with 6 examples of different types of simple and complex JSON structures in Flutter

Java 2.44% Objective-C 4.95% Dart 92.61%

parsingjson-flutter's Introduction

Parsing complex JSON in Flutter

Gives a detailed explanation of working with simple and complex JSON structures using dart:convert library in Flutter along with a sample project with 6+ examples to experiment with.

Tutorial

Read the Medium article here

Types of JSON structures

  • Simple map
  • Simple structure with arrays
  • Simple nested structures
  • Nested structure with Lists
  • List of maps
  • Complex nested structures
  • Simple structure with Nested Maps [parsed using json_serializable library]
  • Demo for Network Calls with examples for GET, POST methods along with FutureBuilder and .then()

How to work with Network Calls?

Not covered in the article

Let's take this API as an example.

Take a look at post_model.dart for the model class and utility methods. I produced it using this converter tool

GET getAllPosts

//services.dart
Future<List<Post>> getAllPosts() async {
  final response = await http.get(url);
  return allPostsFromJson(response.body);
}
// As a part of your UI widget, e.g body of Scaffold widget
FutureBuilder<List<Post>>(
            future: getAllPosts(),
            builder: (context, snapshot) {
              if(snapshot.hasData)
                return Text('Title from Post JSON : ${snapshot.data[0].title}');
              else
                return CircularProgressIndicator();
            }
        )

GET getPost (to get a particular POST by id)

//services.dart
Future<Post> getPost() async{
  final response = await http.get('$url/1'); // the number is the id of the item being accessed
  return postFromJson(response.body);
}
// As a part of your UI widget, e.g body of Scaffold widget
FutureBuilder<Post>(
            future: getPost(),
            builder: (context, snapshot) {
              if(snapshot.hasData)
                return Text('Title from Post JSON : ${snapshot.data.title}');
              else
                return CircularProgressIndicator();
            }
        )

POST createPost

//services.dart
Future<http.Response> createPost(Post post) async{
  final response = await http.post('$url',
      headers: {
        HttpHeaders.contentTypeHeader: 'application/json'
      },
      body: postToJson(post)
  );

  return response;
}
  //call this function when you want to create a new post
 callAPI(){
    Post post = Post(
      body: 'Testing body body body',
      title: 'Flutter jam6'
    ); // creating a new Post object to send it to API
    
    createPost(post).then((response){
        if(response.statusCode > 200)
          print(response.body);
        else
          print(response.statusCode);
    }).catchError((error){
      print('error : $error');
    });
    
  }

parsingjson-flutter's People

Contributors

poojab26 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.