Skip to main content

Flutter Development (Day -06)


  • In this blog we are going to discuss about Network data that how to get the data form the network in the form of Json.
  • All source code : Click Here.

lib/main.dart:

import 'package:allaboutflutterblog/tuts/http_networking.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: HttpNetworking(),
      debugShowCheckedModeBanner: false,
      // Showing the Themes we will talk themes in later part of the tutorial
      theme: ThemeData(
        primaryColor: Colors.red,
        primarySwatch: Colors.blue,
      ),
    ),
  );
}

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

class HttpNetworking extends StatefulWidget {
  @override
  _HttpNetworkingState createState() => _HttpNetworkingState();
}

class _HttpNetworkingState extends State<HttpNetworking> {
  List posts = [];
  @override
  void initState() {
    super.initState();
    this.getData();
  }

  Future getData() async {
    String url = "https://jsonplaceholder.typicode.com/posts";
    http.Response response = await http.get(url);
    setState(() {
      posts = json.decode(response.body);
    });
    return posts;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Networking"),
        backgroundColor: Colors.redAccent,
        centerTitle: true,
      ),
      body: ListView.builder(
        itemCount: posts.length,
        itemBuilder: (context, i) {
          return Column(
            children: <Widget>[
              Card(
                child: ListTile(
                  leading: CircleAvatar(
                    child: Text("${posts[i]['id']}"),
                  ),
                  title: Text("${posts[i]['title']}"),
                  subtitle: Text("${posts[i]['body']}"),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}

Explanation:

  • Add http package to pubspec.yaml file.
  •  import dart:async and dart:convert for doing asynchronous tasks and convertion/decoding the fetched json data.
  •  Here we are returning the future objects (what is Future Async and Await):
  •  Future is nothing but promise in Javascript. When we ask for something(like network request) it will give a promise that it will return the output data in future time at the time of doing that task we don't need to wait for the output data at that time we will do some other task where async and await comes into picture.
  •  SetState() is used to refresh the screen
  •  In the initState() we are calling the function which returns the data of the from the network.Here getData();
  •  Then in the body we are going to return the data with the help of ListView.

Follow me on:
Twitter    Github     LinkedIn



Comments

Post a Comment

Popular posts from this blog

Flutter Development (Day-00): This is the First Post from this Blog.This blog will tell you about Flutter.If you don't know about it then the upcoming posts will explain you everything.This is 30 day tutorial which explains about Flutter for absolute beginners. prerequsites:  If you are enthusiastic and Curious. All source code :  Click Here . If you are following this tutorial / blog please leave your opinion 👀 / say Hii ✋/ Your e-mail in the comment section that I can improve myself and it will gives boost for me to do more tutorials. If you have any doubts feel free to ask me in the comment section or email me ramubugudi4@gmail.com I can definetly give reply. Follow me on:   Twitter   Github     LinkedIn
Flutter Development (Day-16): All source code :  Click Here . Sorry for the last two days I haven't posted anything because I am ready to leave my hometown to attend my college.Is there anyone from AndhraPradesh or Tamilnadu .If yes please provide your city name in the comment section. In this post we are going to proceed the next level of our todo app.We are going add animations to our app,Code clean Up and some UI changes. We add the animation to our FloationgActionButton.You may ask why you haven't add animations to others as well? I tried it but it is looking weird. Adding animations to  FloationgActionButton is very much similar to our previous basic animation tutorial. From the next we are going to deal with some of the important widgets which we will be using in further tutorials. If you are following this tutorial / blog please leave your opinion 👀 / say Hii ✋/ Your e-mail in the comment section that I can improve myself and it will gives boost for me t...
Flutter Development (Day-02): This is the 3rd post from this blog.Today we are going to see some of the basic widgets in flutter and how ,where to use those widgets. In Flutter everything is a widget like view in Android.Text,Padding,Margin are also widgets in Flutter. All source code :  Click Here Scaffold: Implements the basic Material Design visual layout structure. This class provides APIs for showing drawers, snack bars, and bottom sheets. Appbar: A Material Design app bar. An app bar consists of a toolbar and potentially other widgets, such as a TabBar and a FlexibleSpaceBar. Container: A convenience widget that combines common positioning of the widgets. Row: Layout a list of child widgets in the horizontal direction. Column: Layout a list of child widgets in the vertical direction. Text: A run of text with a single style. Image: A widget that displays an image. Icon: A Material Design icon. Raised...