【问题标题】:Displaying API data using FutureBuilder使用 FutureBuilder 显示 API 数据
【发布时间】:2021-07-25 16:12:34
【问题描述】:

我想从复杂的 json API 中获取数据并在 Flutter 未来构建器中显示数据。

这是json的示例

 {
    "hours": [
        {
            "time": "2021-03-23T00:00:00+00:00",
            "waveHeight": {
                "icon": 1.35,
                "meteo": 1.25,
                "noaa": 1.28,
                "sg": 1.25
            }
        },
{
            "time": "2021-03-23T00:00:00+00:00",
            "waveHeight": {
                "icon": 1.35,
                "meteo": 1.25,
                "noaa": 1.28,
                "sg": 1.25
            }
        },
    ],
}

这是从 API 获取数据的函数

Future getJsonData() async {
    String url2 =
        'https://api.stormglass.io/v2/weather/point?lat=5.9774&lng=80.4288&params=waveHeight&start=2021-03-23&end2021-03-24';
    
    String apiKey =
        '0242ac130002-248f8380-7a54-11eb-8302-0242ac130002';
    print('0');

    
    Response response = await get(Uri.parse(url2),
        headers: {HttpHeaders.authorizationHeader: apiKey});

   
    final _extractedData = json.decode(response.body) as Map<String, dynamic>;

    List<Wave> _data = [];
    List<Wave> _fetchedData = [];

    _extractedData['hours'].forEach((value) {
      _fetchedData.add(Wave(
        time: value['time'],
        icon: value['icon'],
        meteo: value['meteo'],
        noaa: value['noaa'],
        sg: value['sg'],
      ));
    });

    _data = _fetchedData;

    print(_data);

    return _data;
  }

数据在控制台打印如下

/flutter ( 4879): [Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', Instance of 'Wave', In

下面是未来建造者

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("API"),
      ),
      body: Container(
        child: FutureBuilder(
            future: getJsonData(),
            builder: (context, AsyncSnapshot<dynamic> snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data[0]['time']);
              } else {
                return CircularProgressIndicator();
              }
            }),
      ),
    );
  }

当我运行应用程序时,显示以下错误

The following NoSuchMethodError was thrown building FutureBuilder<dynamic>(dirty, state: _FutureBuilderState<dynamic>#e19f8):
Class 'Wave' has no instance method '[]'.
Receiver: Instance of 'Wave'
Tried calling: []("time")

以下是wave类

class Wave {
  final String time;
  final double icon;
  final double meteo;
  final double noaa;
  final double sg;

  Wave({
    this.time,
    this.icon,
    this.meteo,
    this.noaa,
    this.sg,
  });

  factory Wave.fromJson(Map<String, dynamic> json) {
    return Wave(
        time: json['time'],
        icon: json['icon'],
        meteo: json['mateo'],
        noaa: json['noaa'],
        sg: json['sg']);
  }
}

我想获取数据并将其打印在颤振列表视图中

【问题讨论】:

  • 试试 snapshot.data[0].time
  • 返回文本(snapshot.data[0].time); data[0] 不是 Map 它是 Wave 类
  • Text(snapshot.data[0].time) 在控制台中显示“Wave”实例

标签: json flutter flutter-layout hybrid-mobile-app flutter-web


【解决方案1】:

这是因为您从 getJsonData 返回的数据不是地图,而是您创建的“WAVE”模型类。

基本上要访问您的“时间”,您需要更改代码:

return Text(snapshot.data[0]['time']);

return Text(snapshot.data[0].time);

【讨论】:

  • 类 'List' 没有实例 getter 'time'。此错误显示
  • 可以在控制台打印快照.data[0]的值吗?
  • snapshot.data[0] 显示类型“Wave”不是“String”类型的子类型
  • 好的,请问您可以编辑您的问题并添加 WAVE 类模型吗?另外,现在尝试打印 snapshot.data[0].time,让我们看看它做了什么
  • 编辑问题
猜你喜欢
  • 2021-02-10
  • 2018-12-22
  • 2021-03-04
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 2023-04-10
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多