How to Populate DropDownButton using Json API in Flutter

In this Google flutter code example we are going to learn how to populate dropdownbutton using remote Json API in flutter.

The code is based on flutter version 1.0.0

Main.dart (App entry point)

import 'package:flutter/material.dart';
import 'week2/dropdownbutton_json_api.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        // This is the theme of your application.
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
      //home: MyHomePage(title: 'Flutter Demo Home Page'),
      home: JsonApiDropdown(),


import 'dart:convert';
import 'package:http/http.dart' as http;

import 'package:flutter/material.dart';

class JsonApiDropdown extends StatefulWidget {
  JsonApiDropdownState createState() {
    return new JsonApiDropdownState();

class JsonApiDropdownState extends State<JsonApiDropdown> {
  Users _currentUser;

  final String uri = '';

  Future<List<Users>> _fetchUsers() async {
    var response = await http.get(uri);

    if (response.statusCode == 200) {
      final items = json.decode(response.body).cast<Map<String, dynamic>>();
      List<Users> listOfUsers =<Users>((json) {
        return Users.fromJson(json);

      return listOfUsers;
    } else {
      throw Exception('Failed to load internet');

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Fetching data from JSON - DropdownButton'),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: <Widget>[
                future: _fetchUsers(),
                builder: (BuildContext context,
                    AsyncSnapshot<List<Users>> snapshot) {
                  if (!snapshot.hasData) return CircularProgressIndicator();
                  return DropdownButton<Users>(
                        .map((user) => DropdownMenuItem<Users>(
                              child: Text(,
                              value: user,
                    onChanged: (Users value) {
                      setState(() {
                        _currentUser = value;
                    isExpanded: false,
                    //value: _currentUser,
                    hint: Text('Select User'),
            SizedBox(height: 20.0),
            _currentUser != null
                ? Text("Name: " +
                    "\n Email: " +
                    "\n Username: " +
                : Text("No User selected"),

/* {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
    "phone": "1-770-736-8031 x56442",
    "website": "",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
  } */

class Users {
  int id;
  String name;
  String username;
  String email;


  factory Users.fromJson(Map<String, dynamic> json) {
    return Users(
      id: json['id'],
      name: json['name'],
      email: json['email'],
      username: json['username'],

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.


Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.