简体   繁体   English

Flutter 如何显示 FutureBuilder 的 snapshot.data?

[英]Flutter how do i display snapshot.data of FutureBuilder?

I want to display the summoner data from riot api, but I get this error 'error: The method '[]' can't be unconditionally invoked because the receiver can be 'null'.我想显示来自 riot api 的召唤师数据,但我收到此错误“错误:无法无条件调用方法“[]”,因为接收器可以为“空”。 got red line under "return Text(snapshot.data['id'])".在“return Text(snapshot.data['id'])”下有红线。

so I tried using '?'所以我尝试使用 '?' to make it conditional, but I'm still getting the same error.使其成为有条件的,但我仍然遇到相同的错误。 Anyone knows how to handle this??有人知道怎么处理吗??

FutureBuilder(
                future: getData(widget.value),
                builder: (context, snapshot) {
                  switch (snapshot.connectionState) {
                    case ConnectionState.none:
                      return Text('none');
                    case ConnectionState.active:
                      return Text('active');
                    case ConnectionState.waiting:
                      return Text('waiting,,,');
                    case ConnectionState.done:
                      return snapshot.hasData
                          ? Text(snapshot.data['id'])
                          : Text('N/A');
                    default:
                      return Text('default');

Future getData(String value) async {
    http.Response response =
        await http.get(Uri.parse('$summonerInfoUrl$value?api_key=$apiKey'));

    if (response.statusCode == 200) {
      String data = response.body;
      var decodeData = jsonDecode(data);
      return decodeData;
    } else {
      print(response.statusCode);
      return Text('error occured');
    }
  }

You need to use the null aware operator !您需要使用空感知运算符! before the square brackets, which will force the value to exclude null.在方括号之前,这将强制该值排除空值。 However, only do this if you are sure the response is null, else it will throw an error.但是,仅当您确定响应为空时才执行此操作,否则会引发错误。

If you are not sure if it is null, use the operator ?如果您不确定它是否为空,请使用运算符? before the brackets, followed by ??在括号之前,然后是?? after the brackets and a fallback value.在括号和后备值之后。 Eg.例如。 snapshot.data?['id'] ?? 'No ID' snapshot.data?['id'] ?? 'No ID' . snapshot.data?['id'] ?? 'No ID'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用存储在 FutureBuilder 的 snapshot.data 中的 JSON 数据(无类)? - How do I use JSON data stored in FutureBuilder's snapshot.data (without class)? 当 snapshot.data 为 null 时,如何再次调用 FutureBuilder? - How do I call FutureBuilder again when snapshot.data is null? Flutter/Dart FutureBuilder - 如何将快照数据存储在变量中并将值传递给其他小部件? - Flutter/Dart FutureBuilder - How to store a snapshot.data in a variable and pass the value to other widget? 如何从 snapshot.data [index] 中的数据创建 map。 颤振/飞镖 - How do I create a map from data inside snapshot.data[index]. Flutter/Dart 如何在 flutter 中的 snapshot.data 之前添加预定义文本 - How do I add a predefined text before the snapshot.data in flutter 如何访问 snapshot.data[index] 中的值。 颤振/飞镖 - How do I access value in snapshot.data[index] . Flutter/Dart 如何仅在 FutureBuilder 获取 snapshot.data 后加载列表? - How to load list only after FutureBuilder gets snapshot.data? 我收到关于 snapshot.data()["~~~"] flutter 的错误 - I get an error with snapshot.data()["~~~"] flutter 为什么 snapshot.data 在 FutureBuilder 中返回 null? - Why snapshot.data returns null in FutureBuilder? 无法访问 Futurebuilder 中的 Snapshot.data - Can't Access The Snapshot.data in Futurebuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM