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.个人所在的团队已经选择 ...
随机推荐
- jenkins配合dockerfile部署项目
前言 本节需要对jenkinsfile有点了解,对dockerfile有点了解,对shell有点了解,对docker有点了解 执行流程 jenkins拉取代码仓库中的代码 jenkins执行jenki ...
- CCF 2016-12-1 中间数
CCF 2016-12-1 中间数 题目 问题描述 在一个整数序列a1, a2, -, an中,如果存在某个数,大于它的整数数量等于小于它的整数数量,则称其为中间数.在一个序列中,可能存在多个下标不相 ...
- Windows中的消息与消息队列
消息 在Windows中,消自由MSG结构体表示 typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lPar ...
- 并发编程-线程-死锁现象-GIL全局锁-线程池
一堆锁 死锁现象 (重点) 死锁指的是某个资源被占用后,一直得不到释放,导致其他需要这个资源的线程进入阻塞状态. 产生死锁的情况 对同一把互斥锁加了多次 一个共享资源,要访问必须同时具备多把锁,但是这 ...
- Mac 达芬奇【Davinci Resolve】 无法添加媒体
参考 : https://zhidao.baidu.com/question/182613491787331404.html 打开软件,点击默认的未命名项目: 点击左上角图中箭头位置: 选中系统-&g ...
- Java并发多线程面试题 Top 50
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- kubernetes第二章--集群搭建
- Java知识回顾 (11) 异常处理
距离最近的 Java知识回顾系列(10),2019.4.24日,到现在,已经近半年过去了. 感觉,做一件事情,如果有头无尾,实在不好,心里会一直悬着.所以,现在继续上面的内容. 再次声明,正如(1)中 ...
- 【kubernetes】通过rancher2部署k8s
1. K8S相关介绍 十分钟带你理解Kubernetes核心概念 2. 部署rancher # 更新操作系统软件包 yum update -y # 删除历史容器及数据 docker rm -f $(d ...
- mysql 安装与基本管理
目录 mysql 安装与基本管理 MySQL介绍 下载安装 登录设置密码 破解密码 统一字符编码 mysql参数配置 mysql 常用指令 mysql 安装与基本管理 MySQL介绍 MySQL是一个 ...