android - Flutter:未来的构建
问题描述
我只需要一些关于如何使用未来构建的指导。我有一个应用程序应该在我的实时服务器上获取一些数据。数据获取成功,我想在listingview小部件上显示这些数据。但可悲的是,它只是返回 null 而没有错误(我认为)。
在此处输入图像描述-> 请检查图像。
这是我的代码:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:phc_gps_sms/constant.dart';
import 'package:phc_gps_sms/network_utils/api.dart';
class TipsCarousel extends StatelessWidget {
const TipsCarousel({
Key? key,
}) : super(key: key);
_getGuidlines() async {
var res;
res = await Network().getData('/guidlines');
var body = json.decode(res.body)['guidlines'];
print(body);
return body;
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Tips',
style: TextStyle(
fontSize: 22.0,
fontWeight: FontWeight.bold,
),
),
GestureDetector(
onTap: () => print('See All'),
child: Text(
'See All',
style: TextStyle(
color: kPrimaryColor,
fontSize: 16.0,
fontWeight: FontWeight.w600,
letterSpacing: 1.0),
),
)
],
),
),
Container(
height: 300.0,
child: FutureBuilder(
future: _getGuidlines(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Center(
child: Text('snapshot.error'),
);
}
if (snapshot.hasData) {
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Text(snapshot.data['index']['title']);
},
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
)
],
);
}
}
我是新来的,所以请放轻松。
解决方案
推荐阅读
- java-8 - 使用 Java8 运行一个 JEE 应用程序,使用 Java 11 运行另一个
- python - 无法让地图出现在 Python 数据可视化中
- reactjs - 如何调试 API 调用(获取请求)?
- python - XGBClassifier 如何识别 scale_pos_weight 参数的正标签?
- java - ImportError:没有名为 amazonaws 的模块
- java - 如果在 Spring data jpa 中找到实体,有什么方法可以抛出异常?
- jsp - 从不同文件夹的 JSP 服务器下载大文件
- c++ - 多线程模板成员函数错误
- java - 从Java中的mime消息中提取正文
- javascript - 为什么 normalize() 方法在某些情况下不起作用?