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.个人所在的团队已经选择 ...
随机推荐
- ColorTransform调整显示对象的颜色值
ColorTransform调整显示对象的颜色值: /** * * *------------------------------* * | *** 调整显示对象的颜色值 *** | * *----- ...
- 作为一个纯粹数据结构的 Redis Streams
来源:antirez 翻译:Kevin (公众号:中间件小哥) Redis 5 中引入了一个名为 Streams 的新的 Redis 数据结构,吸引了社区极大的兴趣.接下来,我会在社区里进行调查,同用 ...
- 系统开启UAC情形下开机自启动程序如何以管理员权限启动
系统开启UAC情形下开机自启动程序如何以管理员权限启动 题记:本文阐述的是在Windows系统开启UAC的情况下,开机自启动程序需要以管理员权限启动, 系统弹出UAC对话框,用户同意的情形下启动程序 ...
- SpringBoot +MSSQL
____SpringBoot +MSSQL_______________________________________________________________________________ ...
- c# 基于RTMP推流 PC+移动端 拉流播放
网上关于直播相关的文章很多,但是讲解还是不够系统,对于刚刚接触直播开发的朋友实施起来会浪费不少时间.下面结合我自己的经验, 介绍一下直播方面的实战经验. 分成两个部分 第一部分是标题中介绍的基于RTM ...
- JDBC第一个案例
1.概述 JDBC(Java DataBase Connectivity) 是 Java 提供的用于执行 SQL 语句一套 API,可以为多种关系型数据库提供统一访问,由一套用 Java 语言编写的类 ...
- Java电商项目,秒杀,抢购等高并发场景的具体场景和一些概念以及处理思路
这里我借鉴了网上其他大佬的观点: 一:高并发带来的挑战 原因:秒杀抢购会经常会带来每秒几万的高并发场景,为了更快的返回结果给用户. 吞吐量指标QPS(每秒处理请求数),假设一个业务请求响应耗时为100 ...
- 压测工具wrk的编译安装与基础使用
Linux上编译安装: [root@centos ~]# cd /usr/local/src [root@centos ~]# yum install git -y [root@centos ~]# ...
- 服务端php之文件上传
知识点 echo $_SERVER['PHP_SELF']; 自动获取当前文件的路劲(即提交地址为当前页面) 当一个表单有文件域(即文件上传)的时候,method(提交方式)要设置post,这样更加安 ...
- layui.js源码分析
/*! @Title: Layui @Description:经典模块化前端框架 @Site: www.layui.com @Author: 贤心 @License:MIT */ ;!functi ...