I'm trying to display a photo (for now), gotten from a Flickr API, and I'm facing issues. When I run my code, I get an E/ion (11941): ioctl c0044901 failed with code -1: Invalid argument error. I had to deal with a lot of typing issues to get to this point and I'm guessing it's typing errors all the way down, but I can't figure out where.
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Photo>> fetchPhoto() async {
final response = await http
.get(Uri.parse('https://www.flickr.com/services/rest/?method=flickr.photos.search&api_key=d92076c10463fe13f610511210b3dee9&user_id=&tags=tattoo&privacy_filter=1&safe_search=1&content_type=1&format=json&nojsoncallback=1'));
if (response.statusCode == 200) {
return jsonDecode(response.body)['photos']['photo'].map<Photo>((element) => Photo.fromJson(element)).toList();
} else {
throw Exception('Failed to load photo');
}
}
class Photo {
final String owner;
final String id;
final String title;
const Photo({
required this.owner,
required this.id,
required this.title,
});
factory Photo.fromJson(Map<String, dynamic> json) {
return Photo(
owner: json.values.toList()[1],
id: json.values.toList()[0],
title: json.values.toList()[5],
);
}
}
class Homescreen extends StatefulWidget {
const Homescreen({Key? key}) : super(key: key);
@override
_HomescreenState createState() => _HomescreenState();
}
class _HomescreenState extends State<Homescreen> {
late Future<List<Photo>> futurePhoto = fetchPhoto();
@override
void initState() {
futurePhoto = fetchPhoto();
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Center(
child: FutureBuilder<List<Photo>>(
future: futurePhoto,
builder: (context, snapshot) {
if (snapshot.hasData) {
Container(
decoration: BoxDecoration(border: Border.all(color: Colors.black)),
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
child: Column(
children: [
Image.network('https://www.flickr.com/photos/' + snapshot.data![0].owner + '/' + snapshot.data![0].id)
]
)
);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
),
),
],
);
}
}
Also I'm guessing I'm not treating my API request in the best way available, so if someone could give me pointers on how to not make it such a mess, it'd be much appreciated.
Turns out, it was only the debugger sending the invalid argument error, and the reason the code didn't work was that I was constructing the image URL in the wrong way.