来自JSON API的Flutter下拉选项数据 [英] Dropdown option data in flutter from json api

查看:95
本文介绍了来自JSON API的Flutter下拉选项数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter开发的新手。

I am newbie to Flutter development.

我想从api检索数据并显示在flutter下拉选项中。

I want to retrieve data from the api and show in the flutter drop down options.

Api-< a href = http://webmyls.com/php/getdata.php rel = nofollow noreferrer> http://webmyls.com/php/getdata.php

请帮助我使用代码来显示api中的数据

Kindly help me with the code to show the data from the api

谢谢

推荐答案

我仅使用您的代码,仅编辑一个行,它的工作原理就像是魅力。

I am using your code only and edit just one line and it works like charm.

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

void main() => runApp(MaterialApp(
      title: "Hospital Management",
      home: MyApp(),
    ));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _mySelection;

  final String url = "http://webmyls.com/php/getdata.php";

  List data = List(); //edited line

  Future<String> getSWData() async {
    var res = await http
        .get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
    var resBody = json.decode(res.body);

    setState(() {
      data = resBody;
    });

    print(resBody);

    return "Sucess";
  }

  @override
  void initState() {
    super.initState();
    this.getSWData();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("Hospital Management"),
      ),
      body: new Center(
        child: new DropdownButton(
          items: data.map((item) {
            return new DropdownMenuItem(
              child: new Text(item['item_name']),
              value: item['id'].toString(),
            );
          }).toList(),
          onChanged: (newVal) {
            setState(() {
              _mySelection = newVal;
            });
          },
          value: _mySelection,
        ),
      ),
    );
  }
}


您想实现其他目标吗?

Do you want to achieve something else?

这篇关于来自JSON API的Flutter下拉选项数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆