flutter 从接口获取json数据显示到页面
如题,在前端,是个很简单的ajax请求,json的显示,取值都很方便,换用dart之后,除了层层嵌套写的有点略难受之外,还有对json的使用比js要麻烦
1. 可以参照 flutter-go 先封装一下get和post请求,net_utils.dart
2. 发起请求
import 'package:flutter/material.dart';
import 'package:app/api/main.dart';
import 'package:app/utils/net_utils.dart';
import 'package:fluttertoast/fluttertoast.dart'; class SearchPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _SearchPageState();
}
} class _SearchPageState extends State<SearchPage> {
TextEditingController _searchController = new TextEditingController(); List _lists = [];
List _histryLists = []; @override
void initState() {
super.initState();
this._hotSearch(); // 这里,在页面初始化的时候请求列表接口
} void _hotSearch() {
NetUtils.get('http://localhost:3000/search').then((res) => { // 这里get后面是根据前面封装的请求来写的,自行替换url和参数
// print( res['data'])
setState(() {
_lists = res['data']; // 把从接口获取的列表赋值到_list
})
});
} void updateSearch(String keyword) {
print(keyword);
setState(() {
_searchController.text = keyword; // 这个点击搜索把值赋值到input搜索框的
});
} void _searchMusic(){ // 点击搜索
if(_searchController.text==''){
Fluttertoast.showToast(
msg: "关键词不能为空",
gravity: ToastGravity.CENTER,
timeInSecForIos: 1,
);
}else{
NetUtils.get(Api.searchApi(), {"keywords":_searchController.text}).then((res) => {
print(res['result']['songs'])
});
}
} Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextFormField(
autofocus: true,
controller: _searchController,
style:TextStyle(color: Colors.white),
decoration: InputDecoration.collapsed(
hintText: "请输入关键词",
hintStyle: TextStyle(color: Colors.white70)),
),
actions: <Widget>[
//导航栏右侧菜单
IconButton(
icon: Icon(Icons.search, color: Colors.white),
onPressed: () {
_searchMusic();
}),
],
),
body: Container(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('历史记录'),
Icon(IconData(0xe662, fontFamily: 'iconfont')),
],
),
SizedBox(
width: 350.0,
height: 50.0,
child:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _histryLists.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: Padding(
padding: EdgeInsets.all(5.0),
child: Chip(
label: Text(_histryLists[index]),
),
),
onTap: () => {
updateSearch(_histryLists[index])
},
);
},
)),
Padding(
padding: EdgeInsets.only(top: 30, bottom: 20),
child: Column(
children: <Widget>[
Row(
children: <Widget>[Text('热搜榜')],
),
SizedBox(
height: 520.0,
child: ListView.builder( // 这里把从接口请求的列表数据展示到页面上
shrinkWrap: true,
itemCount: _lists.length,
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
child: Padding(
padding: EdgeInsets.all(10),
child: Flex(
direction: Axis.horizontal,
children: <Widget>[
Expanded(
flex: 1,
child: Text(
(index + 1).toString(),
style: TextStyle(
color: Colors.red, fontSize: 18),
)),
Expanded(
flex: 8,
child: Column(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text(
_lists[index]['searchWord'],
style: TextStyle(
color: Colors.grey[900],
fontSize: 16)),
),
Align(
alignment: Alignment.centerLeft,
child: Text(
_lists[index]['content'],
style: TextStyle(
color: Colors.grey[500])),
),
],
),
)
],
),
),
onTap: () =>
updateSearch(_lists[index]['searchWord']),
);
}),
)
],
),
)
],
)),
);
}
}
更多详细请看:https://github.com/leitingting08/NeteaseCloudMusicFlutter/blob/master/lib/pages/home/search.dart
flutter 从接口获取json数据显示到页面的更多相关文章
- .net从网络接口地址获取json,然后解析成对象(一)
整理代码,今天遇到一个问题,就是从一个场景接口获取json,然后解析成对象.之前的时候都好好的,这次返回的json字符串里,由于字符编码的问题,格式上不能转换.一直以为是解析的过程编码有误,试了utf ...
- Vue学习笔记十三:Vue+Bootstrap+vue-resource从接口获取数据库数据
目录 前言 SpringBoot提供后端接口 Entity类 JPA操作接口 配置文件 数据库表自动映射,添加数据 写提供数据的接口 跨域问题 前端修改 效果图 待续 前言 Vue学习笔记九的列表案例 ...
- JS-利用ajax获取json数据,并传入页面生成动态tab
封装好的:ajax.js function ajax(url, fnSucc,fnFaild){ //1[创建] if(window.XMLHttpRequest){ var oAjax = new ...
- [N久以前发布的文章]php 获取yahoo股票csv数据并封闭成为接口输出json数据
思路 先从yahoo接口获取数据,再定义接口,转化成为json输出.只供卡通网(kt5.cn)使用 stock.php 接口处理代码 <?php header("Content-Typ ...
- NodeJs本地搭建服务器,模拟接口请求,获取json数据
最近在学习Node.js,虽然就感觉学了点皮毛,感觉这个语言还不错,并且也会一步步慢慢的学着的,这里实现下NodeJs本地搭建服务器,模拟接口请求,获取json数据. 具体的使用我就不写了,这个博客写 ...
- 请求接口获取的json 字符串 前后不能有 双引号
请求接口获取的json 字符串 前后不能有 双引号 否则JSON.parse 转换会报错
- 豆瓣爬虫——通过json接口获取数据
最近在复习resqusts 爬虫模块,就重新写了一个豆瓣爬虫,这个网页从HTML 源码上来看是没有任何我想要的信息的,如下图所示: 这是网页视图,我在源码中查找影片信息,没有任何信息,如图: 由此我判 ...
- ajax获取json数据及实现跨域请求
最近想练习一下ajax获取json数据 , 首先上网找一些在线的可用来测试的接口. -----------------------------------------------------这里是接口 ...
- (六)Net Core项目使用Controller之一 c# log4net 不输出日志 .NET Standard库引用导致的FileNotFoundException探究 获取json串里的某个属性值 common.js 如何调用common.js js 筛选数据 Join 具体用法
(六)Net Core项目使用Controller之一 一.简介 1.当前最流行的开发模式是前后端分离,Controller作为后端的核心输出,是开发人员使用最多的技术点. 2.个人所在的团队已经选择 ...
随机推荐
- js node md5模块使用问题
问题描述:md5(123456)得到的结果不是正确的. why? 问题查找: 1)安装路径问题: yarn add md5(md5模块在npmjs中显示每周download人数高达百万,有问题还这么多 ...
- TypeScript之函数
1.函数声明 与javascript一样,ts的函数声明也分为两种:函数声明,函数表达式 1)函数声明: function fn(age:number):string{ return `age is ...
- Ubuntu /etc/security/limits.conf 不生效问题
一.问题描述 修改 /etc/security/limits.conf ,重启之后不生效 内容如下: * soft nofile * hard nofile root soft nofile root ...
- 用PHP写PHP7扩展,超级简单对吧!
[图片打不开,请用代理] 介绍: PHP扩展是编译库,它允许在您的PHP代码中使用特定的功能(主要是使用C编写的php扩展). 例如,您需要使用PHP使用SQLite3,您可以实现自己的方法和功能来连 ...
- 用户在浏览器输入URL回车之后,浏览器都做了什么
在直接列出执行的步骤之前先来普及几个知识,相信了解完这些知识之后会对前后端的交互有更深入的理解. 1.TCP连接 TCP:Transmission Control Protocol, 传输控制协议,是 ...
- java git .gitignore常用规则
# Created by .ignore support plugin (hsz.mobi).gitignore # Operating System Files *.DS_Store Thumbs. ...
- 【大数据技术能力提升_1】python基础
.caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...
- web项目中添加定时任务
1.在web.xml中添加servlet <servlet> <servlet-name>StatisticInitServlet</servlet-name> & ...
- mysql学习之基础篇05
mysql中的统计函数: 1. 查询商品价格中最高的价格: select max(shop_price) from goods; 2. 查询商品价格中最低的价格: select min(shop_pr ...
- Linux系统下RAID5和RAID10的磁盘阵列配置
前提了解:1988年由加利福尼亚大学伯克利分校发表的文章首次提到并定义了RAID,当今CPU性能每年可提升30%-50%但硬盘仅提升7%,渐渐的已经成为计算机整体性能的瓶颈,并且为了避免硬盘的突然损坏 ...