Support
Quality
Security
License
Reuse
kandi has reviewed cascading_ext and discovered the below as its top functions. This is intended to give you an instant insight into cascading_ext implemented functionality, and help decide if they suit your requirements.
Get all kandi verified functions for this library.
Get all kandi verified functions for this library.
Some of the most interesting public classes in the project (so far).
See all related Code Snippets
QUESTION
Error: Property 'body' cannot be accessed on 'Response?' because it is potentially null
Asked 2022-Mar-29 at 00:29so I'am trying to learn dart and flutter and everything went well so far. But now I'am stuck at an error which I cannot handle. I coded a function which is supposed to asynchronously return the actual BTC price from https://blockchain.info/ticker.
Only thing it returns is errors:
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;
^
My Code:
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']
);
}
}
Last things to mention: I'am running the application on a Android Emulator powerd by Android Studio; and please feel free to hand over any advice you have (in terms of code improvement), even if it is not fixing my issue.
ANSWER
Answered 2022-Mar-29 at 00:29To get rid of that error you need to use the bang operator to tell the compiler that snapshot.data
won't be null.
if (statusCode == 200) {
return Text(
"${BTCPrice.fromJson(jsonDecode(snapshot.data!.body)).eur}"); // adding ! on data
}
The unrelated error you mentioned in your comment:
unexpected character (at character 1) <html><head><title>loading...</title></head><body><script type='text/javasc... ^
would be fixed by changing your GET
request from this
final response = await http.get(Uri.https('blockhain.info', 'ticker'));
to this
final response = await http.get(Uri.parse('https://blockchain.info/ticker'));
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Find more information at:
Save this library and start creating your kit
Save this library and start creating your kit
Open Weaver – Develop Applications Faster with Open Source