Answer a question

my code :

  import 'package:http/http.dart' as http;
  import 'dart:convert';
  void main()async {

  var data =await fatchalbum();

   for(int i =0; i>100;i++){
   print(data[i]["userId"]);
   print(data[i]["id"]);
   print(data[i]["title"]);
 }
}

Future fatchalbum()async{
final url ="https://jsonplaceholder.typicode.com/albums";
final res = await http.get(Uri.parse(url));
if(res.statusCode==200){
var obj = json.decode(res.body);
  return obj;
 }else{
 throw Exception("error");
 }
}

If i write in the "main function " print("hi"); it can run in debuge console just "hi"
It does not display the rest of the data

when i run the code , This error appears to me in Debuge console

 Failed to save Chrome preferences: FileSystemException: Cannot open file, path = 'C:\Users\dell\AppData\Local\Temp\flutter_tools.ab04b1f8\flutter_tools_chrome_device.49e73a42\Default\Cache\data_0' (OS Error: The process cannot access the file because it is being used by another process.
, errno = 32)

Sometimes when I press stop then press Run&Debuge It just shows me, it doesn't show me The ouput I want:

This app is linked to the debug service: ws://127.0.0.1:55071/6_DbR0EpDJg%3D/ws
Launching lib\main.dart on Edge in debug mode...
lib\main.dart:1
Debug service listening on ws://127.0.0.1:55071/6_DbR0EpDJg=/ws
 Running with sound null safety
Connecting to VM Service at ws://127.0.0.1:55071/6_DbR0EpDJg=/ws

Answers

basically your fetchalbum method is returning a List of Maps so the best approach to print each element of List is to use "For in" loop heres the code you should use:-

for (var element in data){
print(var["userId"]);
print(var["id"]);
print(var["title"]);
}

You are doing a little mistake in you approach to print data, you are giving conding in your loop that when i is greater than 100 i > 100, thats basically returns false thats why it doesnt prints any thing.

second approach to print data is :-

for(int i = o; i <data.length ; i++){
//prints from api
}

or

for(int i = 0; i <100 ; i++){
//prints from api }
Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐