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=/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
CodePudding user response:
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 }
CodePudding user response:
It's because you made a mistake in the for
loop. It should be i<100
in the for loop not i>100
.
...
for(int i =0; i<100;i ){
print(data[i]["userId"]);
print(data[i]["id"]);
print(data[i]["title"]);
}
...
NOTE: i<100
in the for loop means, while the condition is true keep incrementing until it becomes false.
CodePudding user response:
You should avoid specific length in condition, though it's network call and in your condition, you get data when i's value greater than 100. So, you could reverse your condition to resolve the problem.
for(int i =0; i<data.length;i ){
print(data[i]["userId"]);
print(data[i]["id"]);
print(data[i]["title"]);
}