Error: Property 'body' cannot be accessed on 'Response?' because it is potentially null
Error: Property 'body' cannot be accessed on 'Response?' because it is potentially null
所以我正在尝试学习飞镖和扑动,到目前为止一切顺利。但是现在我陷入了一个我无法处理的错误。我编写了一个函数,该函数应该异步 return 来自 https://blockchain.info/ticker.
的实际 BTC 价格
唯一 return 是 错误 :
Error: Property 'body' cannot be accessed on 'Response?' because it is potentially null.
- 'Response' is from 'package:http/src/response.dart' ('/D:/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.13.4/lib/src/response.dart').
Try accessing using ?. instead.
return Text("${BTCPrice.fromJson(jsonDecode(snapshot.data.body)).eur}");
^^^^
/D:/flutter/packages/flutter/lib/src/widgets/async.dart:242:12: Context: 'data' refers to a property so it couldn't be promoted.
See http://dart.dev/go/non-promo-property
final T? data;
^
我的代码:
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<http.Response> fetchBTCPrice() async {
final response = await http.get(Uri.https('blockhain.info', 'ticker'));
return response;
}
Widget buildBTCPrice() {
return FutureBuilder<http.Response>(
future: fetchBTCPrice(),
builder: (context, snapshot) {
if (snapshot.hasData) {
int? statusCode = snapshot.data?.statusCode;
if (statusCode == 200) {
return Text("${BTCPrice.fromJson(jsonDecode(snapshot.data.body)).eur}");
}
return Text('$statusCode');
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return CircularProgressIndicator();
},
);
}
class BTCPrice {
final double eur;
BTCPrice({required this.eur});
factory BTCPrice.fromJson(Map<String, dynamic> json) {
print(json);
return BTCPrice(
eur: json['eur']['15m']
);
}
}
最后要提的事情:我是运行 Android 模拟器上的应用程序,由 Android Studio 提供支持;请随时提供您的任何建议(在代码改进方面),即使它不能解决我的问题。
要消除该错误,您需要使用 告诉编译器 snapshot.data
不会为 null。
if (statusCode == 200) {
return Text(
"${BTCPrice.fromJson(jsonDecode(snapshot.data!.body)).eur}"); // adding ! on data
}
编辑
您在评论中提到的无关错误:
unexpected character (at character 1) <html><head><title>loading...</title></head><body><script type='text/javasc... ^
将通过更改此
中的 GET
请求来解决
final response = await http.get(Uri.https('blockhain.info', 'ticker'));
至此
final response = await http.get(Uri.parse('https://blockchain.info/ticker'));
所以我正在尝试学习飞镖和扑动,到目前为止一切顺利。但是现在我陷入了一个我无法处理的错误。我编写了一个函数,该函数应该异步 return 来自 https://blockchain.info/ticker.
的实际 BTC 价格唯一 return 是 错误 :
Error: Property 'body' cannot be accessed on 'Response?' because it is potentially null.
- 'Response' is from 'package:http/src/response.dart' ('/D:/flutter/.pub-cache/hosted/pub.dartlang.org/http-0.13.4/lib/src/response.dart').
Try accessing using ?. instead.
return Text("${BTCPrice.fromJson(jsonDecode(snapshot.data.body)).eur}");
^^^^
/D:/flutter/packages/flutter/lib/src/widgets/async.dart:242:12: Context: 'data' refers to a property so it couldn't be promoted.
See http://dart.dev/go/non-promo-property
final T? data;
^
我的代码:
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<http.Response> fetchBTCPrice() async {
final response = await http.get(Uri.https('blockhain.info', 'ticker'));
return response;
}
Widget buildBTCPrice() {
return FutureBuilder<http.Response>(
future: fetchBTCPrice(),
builder: (context, snapshot) {
if (snapshot.hasData) {
int? statusCode = snapshot.data?.statusCode;
if (statusCode == 200) {
return Text("${BTCPrice.fromJson(jsonDecode(snapshot.data.body)).eur}");
}
return Text('$statusCode');
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return CircularProgressIndicator();
},
);
}
class BTCPrice {
final double eur;
BTCPrice({required this.eur});
factory BTCPrice.fromJson(Map<String, dynamic> json) {
print(json);
return BTCPrice(
eur: json['eur']['15m']
);
}
}
最后要提的事情:我是运行 Android 模拟器上的应用程序,由 Android Studio 提供支持;请随时提供您的任何建议(在代码改进方面),即使它不能解决我的问题。
要消除该错误,您需要使用 snapshot.data
不会为 null。
if (statusCode == 200) {
return Text(
"${BTCPrice.fromJson(jsonDecode(snapshot.data!.body)).eur}"); // adding ! on data
}
编辑
您在评论中提到的无关错误:
unexpected character (at character 1) <html><head><title>loading...</title></head><body><script type='text/javasc... ^
将通过更改此
中的GET
请求来解决
final response = await http.get(Uri.https('blockhain.info', 'ticker'));
至此
final response = await http.get(Uri.parse('https://blockchain.info/ticker'));