Lanka Developers Community

    Lanka Developers

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Shop

    Flutter Http call

    Flutter
    api call array http call json online
    1
    1
    239
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Muki Bwoi
      Muki Bwoi last edited by Muki Bwoi

      import 'package:flutter/material.dart';
      import 'dart:convert';
      import 'package:http/http.dart' as http;
      
      Future<Profile> fetchProfile() async {
        final response =
            await http.get('https://hpb.health.gov.lk/api/get-current-statistical');
      
        if (response.statusCode == 200) {
          return Profile.fromJson(jsonDecode(response.body));
        } else {
          throw Exception('Failed to load Data');
        }
      }
      
      class Profile {
        String data;
      
        Profile({this.data});
      
        factory Profile.fromJson(Map<String, dynamic> json) {
          return Profile(data : json["data"]);
        }
      }
      
      void main() {
        runApp(new MaterialApp(
          home: new Button(),
        ));
      }
      
      class MyApp extends StatefulWidget {
        @override
        _MyAppState createState() => _MyAppState();
      }
      
      class _MyAppState extends State<MyApp> {
        Future<Profile> futureProfile;
      
        @override
        void initState() {
          futureProfile = fetchProfile();
          super.initState();
        }
      
        @override
        Widget build(BuildContext context) {
          return FutureBuilder<Profile>(
            future: futureProfile,
            // ignore: missing_return
            builder: (contex, snapshot) {
              if (snapshot.hasData) {
                return Scaffold(
                    body: Center(child: Container(child: Text(snapshot.data.data))));
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              }
      
              return Scaffold(
                body: Center(
                  child: CircularProgressIndicator(
                    backgroundColor: Colors.white,
                    strokeWidth: 5,
                  ),
                ),
              );
            },
          );
        }
      }
      
      class Button extends StatefulWidget {
        @override
        _ButtonState createState() => _ButtonState();
      }
      
      class _ButtonState extends State<Button> {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Center(
              child: Container(child: RaisedButton(
                onPressed: () {
                  Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                    return MyApp();
                  }));
                },
              )),
            ),
          );
        }
      }
      
      
      

      මේ කෝඩ් එක යට කියල තියෙන විදිහට වෙනස් කරන්නෙ කොහොමද ?

      flutter වල ඉන්ටර්නෙට් එක හරහා API කෝල් කරනකොට ජේසන් ෆයිල් එකේ ඇරේ එකක් ඇතුලේ තියෙන ඩේටා කෝල් කරන්නෙ කොහොමද ?

      උදාහරනයක් විදිහට මේ වගේ එකකින්.
      https://hpb.health.gov.lk/api/get-current-statistical

      මේකේ ඩේටා තියෙන්නෙ data කියන ඇරේ එක ඇතුලේ.
      ඒක කෝල් කරන්නෙ කොහොමද?:confused: :confused: :confused:

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post

      4
      Online

      3.6k
      Users

      1.3k
      Topics

      5.3k
      Posts

      • Privacy
      • Terms & Conditions
      • Donate

      © Copyrights and All right reserved Lanka Developers Community

      Powered by Axis Technologies (PVT) Ltd

      Made with in Sri Lanka

      | |