首页 > 解决方案 > 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(),
          );
        },
      ),
    )
  ],
);
  }
}

我是新来的,所以请放轻松。

标签: androidflutterdart

解决方案


推荐阅读