<分区>
我想从 API 中检索数据并将其显示在 flutter 下拉选项中。
API - http://webmyls.com/php/getdata.php
如何编写代码来显示来自 API 的数据?
<分区>
我想从 API 中检索数据并将其显示在 flutter 下拉选项中。
API - http://webmyls.com/php/getdata.php
如何编写代码来显示来自 API 的数据?
最佳答案
我只使用您的代码并且只编辑一行,它就像魅力一样。
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,
),
),
);
}
}
关于dart - 来自 json api 的下拉选项数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094031/
相关文章:
flutter - 如何使用 'CupertinoFullscreenDialogTransition' ?
android - 为什么我的小部件的构建方法认为我返回 NULL?
flutter - 在ListTile中的字幕内实现if语句
sqlite - 不使用 Flutter 的 SQFlite 关闭数据库?
user-interface - 容器的 Flutter onTap 方法
image - Flutter CachedNetworkImageProvider 没有加载微调器
flutter - 失败: Build failed with an exception (Shared preference and gradle wrapper properties)