I am getting some data from API in flutter. I am trying to decode the data using json.decode() but this decode function gives me the following error:
Closure: () => String from Function 'toString'
Here's my code:
Future<Product> createOrder() async {
var client = new http.Client();
var productModel = null;
try {
var response = await client
.get(Uri.https('butterbasket.onrender.com', Strings.createOrderUrl));
if (response.statusCode == 200) {
var body = response.body;
print("Body: $body");
var jsonMap = json.decode(body);
var productModel = Product.fromJson(jsonMap);
}
} catch (e) {
print("Exception: ${e.toString}");
}
return productModel;
}
Here is the Error Debug Console:
You are running into issues because the data you are loading in is an array, but the model is an object. You'll need to do something like the following:
final List jsonProducts = json.decode(body)
final List<Product> products =
jsonProducts.map((jsonProduct) => Product.fromJson(jsonProduct)).toList();
and then if you only need the first item you can do:
final Product product = products[0]
But don't forget to check if the array is big enough.
Your Future function must return an optional Product?, otherwise your future will never find a result as you are declaring it inside the function.
Instead of:
Future<Product> createOrder() async {}
Use:
Future<Product?> createOrder() async {}
Finally your async snapshot and FutureBuilder type should be of type <Product?>.
Related
My goal is to do a simple BitcoinApp. I am trying to get a method that is in the MyHomePageState class to call a method that I have in another class. When I compile and click on the button to give me the bitcoin info of USD I get the error of setState() callback argument returned to Future. Any advice or alternative that you can suggest me? I'm new to Flutter and adjusting.Here is my code:
///This piece of code is located in MyHomePageState Class
BitcoinCurrency _bitcoin = BitcoinCurrency();
void _getUSDBitcoin(){
setState(() async{
_bitcoin.usdBitcoin();
});
}
///This is the class I have outside of MyHomePageState Class.
class BitcoinCurrency {
///Variables we want for the information
String _disclaimer = "N/A";
String _time = "N/A";
String _currencyBitcoin = "N/A";
///Getters for our variables
get disclaimer => _disclaimer;
get time => _time;
get currencyBitcoin => _currencyBitcoin;
///Methods()
void usdBitcoin() async{
var url = Uri.https('api.coindesk.com', '/v1/bpi/currentprice.json');
var response = await http.get(url);
var httpBody = response.body;
var decoded = json.decode(httpBody);
_disclaimer = decoded['disclaimer'];
_time = decoded['time']['updated'];
_currencyBitcoin = decoded['bpi']['USD']['rate'];
}
}
You can convert usdBitcoin void method to Future<void>
Future<void>? usdBitcoin() async{
var url = Uri.https('api.coindesk.com', '/v1/bpi/currentprice.json');
var response = await http.get(url);
var httpBody = response.body;
var decoded = json.decode(httpBody);
_disclaimer = decoded['disclaimer'];
_time = decoded['time']['updated'];
_currencyBitcoin = decoded['bpi']['USD']['rate'];
}
And call setState like
usdBitcoin().then((value) => setState(() {}));
setState can't be an async function. usdBitcoin has to be a Future method, so you have to call it before the setState starts.
usdBitcoin method:
Future usdBitcoin() async{
var url = Uri.https('api.coindesk.com', '/v1/bpi/currentprice.json');
var response = await http.get(url);
var httpBody = response.body;
var decoded = json.decode(httpBody);
_disclaimer = decoded['disclaimer'];
_time = decoded['time']['updated'];
_currencyBitcoin = decoded['bpi']['USD']['rate'];
}
In initState:
usdBitcoin().then(
(value) => setState(
() {
},
),
)
Objective is to convert a String to List using map and return the value to a function call.
I am using SharedPreferences to save a list of object called where in I save the data at a point and get the data when it is to be put on view.
The below block is the function where the error is occurring.
void getData() async {
final prefs = await SharedPreferences.getInstance();
final String taskString = prefs.getString('task_data').toString();
List<Task> tasksData = Task.decode(taskString);
_tasks = tasksData;
notifyListeners();
}
decode() looks basically does the conversion.
static List<Task> decode(String tasks) {
return (jsonDecode(tasks) as List<dynamic>).map<Task>((task) {
return Task.fromJson(task);
}).toList();
It advises to check for null condition in type cast of decode(). But on performing the check, it gives the same error.
your response might be not a proper map so it cannot decode that data using the jsonDecode function so it returns Null, so you can use your function like this might be helpful for you :
static List<Task> decode(String tasks) {
var data = (jsonDecode(tasks) as List<dynamic>?);
if(data != null){
return (jsonDecode(tasks) as List<dynamic>?)!.map<Task>((task) {
return Task.fromJson(task);
}).toList();
} else {
return <Task>[];
}
}
How do I set up a flutter method to return a future value that is drawn from the results of a future http post call inside the method?
The example code below is making a call to a web URL to add a new product. I want this method to return just the Id of the newly created product (i.e. 'name' inside response)
Future<String> add(Product aNewProduct) async {
var aUrl = Uri.parse(dbUrl);
http.post(aUrl,body: toBody(aNewProduct),).then((response) {
var aStr = json.decode(response.body)['name'];
return Future<String>.value(aStr);
});
}
With the code above, the parser is showing the following error/warning...
The body might complete normally, causing 'null' to be returned,
but the return type, 'FutureOr<String>', is a potentially non-nullable type.
(Documentation) Try adding either a return or a throw statement at the end.
Any suggestions on how to fix this?
You can use the await to get the value of a Future or rather your http request. After that you can simple decode it and return your desired behavior.
Future<String> add(Product aNewProduct) async {
var aUrl = Uri.parse(dbUrl);
final response = http.post(
aUrl,
body: toBody(aNewProduct),
);
return json.decode(response.body)['name'];
}
try this:
Future<String> add(Product aNewProduct) async {
var aUrl = Uri.parse(dbUrl);
var response= await http.post(aUrl,body: toBody(aNewProduct),);
if(response.statusCode==200){
var rawData = await response.stream.bytesToString();
Map data=json.decode(rawData);
return data['name'];
}else{
return '';
}
}
It is as simple as putting a return before the http.post statement
I have this future method to get a data from server and using it :
Future<String> get_week() async {
var weekUrl =
'https://xxx/api/controller/matchs/active_week.php';
var weekresponse = await http.get(weekUrl);
var weekdata = await jsonDecode(weekresponse.body);
var weekId = weekdata[0]['w_id'];
return weeId
}
How can i use the value of weekId outside this method?
You can use the await keyword to assign the returned value from the future to variable:
String id = await get_week();
Whenever trying to call future data and trying converting to List, it returns the error
type 'Future' is not a subtype of type 'List'
Tried type-casting, but no help
On HomePage.dart
final getPost = NetworkFile().getPosts();
List posts;
void getPostsList() {
setState(() {
var res = getPost;
posts = res as List<dynamic>;
print(posts);
});
}
On Network.dart
class NetworkFile{
Future<dynamic> getPosts() async {
var response = await http.get('$kBlogURL' + 'posts?_embed');
Iterable resBody = await jsonDecode(response.body.toString());
return resBody;
}
}
You are decoding the response and its a List of type dynamic. There are few method to handle it. You can create a simple PODO class and cast/mapped to it. Or just do like below:
List posts = [];
void getPostsList() async {
final fetchedPosts = await NetworkFile().getPosts();
setState(() {
posts = fetchedPosts;
});
print(posts);
}
Here is a nice article about PODO.
final getPost = NetworkFile().getPosts();
Map posts;
void getPostsList() async {
var res = await getPost;
setState(() {
posts = res as Map<String, dynamic>;
print(posts);
});
}
class NetworkFile {
Future<dynamic> getPosts() async {
var response = await http.get('https://onetechstop.net/wp-json/wp/v2');
var resBody = await jsonDecode(response.body.toString());
return resBody;
}
}