Koneksi flutter ke web service

selamat siang.. saya baru belajar flutter dg menggunakan web service. tapi bingung cara koneksi dari flutter bisa akses ke webservice. ada yang bisa bantu? saya tidak sertakan source code nya karena saya bener2 bingung, di interner masih minim penjelasan nya, mohon bantuanyaa

avatar oceanli
@oceanli

63 Kontribusi 2 Poin

Diperbarui 4 tahun yang lalu

3 Jawaban:

Kalau yang dimaksud akses API cek <a href='https://flutter.dev/docs/cookbook/networking/fetch-data'>dokumentasi flutter bagian fetch data</a>

contoh

<pre> import 'dart:async'; import 'dart:convert';

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

Future&lt;Post&gt; fetchPost() async { final response = await http.get('https://jsonplaceholder.typicode.com/posts/1');

if (response.statusCode == 200) { // If the call to the server was successful, parse the JSON. return Post.fromJson(json.decode(response.body)); } else { // If that call was not successful, throw an error. throw Exception('Failed to load post'); } }

class Post { final int userId; final int id; final String title; final String body;

Post({this.userId, this.id, this.title, this.body});

factory Post.fromJson(Map&lt;String, dynamic&gt; json) { return Post( userId: json['userId'], id: json['id'], title: json['title'], body: json['body'], ); } }

void main() =&gt; runApp(MyApp(post: fetchPost()));

class MyApp extends StatelessWidget { final Future&lt;Post&gt; post;

MyApp({Key key, this.post}) : super(key: key);

@override Widget build(BuildContext context) { return MaterialApp( title: 'Fetch Data Example', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar( title: Text('Fetch Data Example'), ), body: Center( child: FutureBuilder&lt;Post&gt;( future: post, builder: (context, snapshot) { if (snapshot.hasData) { return Text(snapshot.data.title); } else if (snapshot.hasError) { return Text("${snapshot.error}"); }

          // By default, show a loading spinner.
          return CircularProgressIndicator();
        },
      ),
    ),
  ),
);

} } </pre>

avatar hilmanski
@hilmanski

2696 Kontribusi 2139 Poin

Dipost 5 tahun yang lalu

Main ke sini aja lengkap <a href=' https://flutter.dev/docs/cookbook/networking/fetch-data '>Flutter</a>

avatar markeronly
@markeronly

378 Kontribusi 230 Poin

Dipost 5 tahun yang lalu

bisa baca <a href='https://flutter.dev/docs/cookbook/networking/fetch-data'>dokumentasinya disini</a> sesuaikan dengan kebutuhan project Anda, menggunakan packages http method GET atau POST. ini contoh dari dokumentasi flutter:

<pre> import 'dart:async'; import 'dart:convert';

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

Future&lt;Album&gt; fetchAlbum() async { final response = await http.get('https://jsonplaceholder.typicode.com/albums/1');

if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. return Album.fromJson(json.decode(response.body)); } else { // If the server did not return a 200 OK response, // then throw an exception. throw Exception('Failed to load album'); } }

class Album { final int userId; final int id; final String title;

Album({this.userId, this.id, this.title});

factory Album.fromJson(Map&lt;String, dynamic&gt; json) { return Album( userId: json['userId'], id: json['id'], title: json['title'], ); } }

void main() =&gt; runApp(MyApp());

class MyApp extends StatefulWidget { MyApp({Key key}) : super(key: key);

@override _MyAppState createState() =&gt; _MyAppState(); }

class _MyAppState extends State&lt;MyApp&gt; { Future&lt;Album&gt; futureAlbum;

@override void initState() { super.initState(); futureAlbum = fetchAlbum(); }

@override Widget build(BuildContext context) { return MaterialApp( title: 'Fetch Data Example', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( appBar: AppBar( title: Text('Fetch Data Example'), ), body: Center( child: FutureBuilder&lt;Album&gt;( future: futureAlbum, builder: (context, snapshot) { if (snapshot.hasData) { return Text(snapshot.data.title); } else if (snapshot.hasError) { return Text("${snapshot.error}"); }

          // By default, show a loading spinner.
          return CircularProgressIndicator();
        },
      ),
    ),
  ),
);

} } </pre>

avatar happybelajar
@happybelajar

9 Kontribusi 2 Poin

Dipost 4 tahun yang lalu

Login untuk ikut Jawaban