I have an API which send a random photo each time you call it. But if you've seen all the photos recently, it responds a json object saying that no more photo is available. In my Flutter App, fetching the photo everytime the button is pressed works fine (thx StackOverflow) but i honestly don't see how to handle when the response is a json, because my flutter app can't know when the response will be json, so I can't use a model. Here is the working code if a photo is available :
import 'package:flutter/material.dart';
import 'dart:math';
class ImageDialog extends StatelessWidget {
@override
@override
Widget build(BuildContext context) {
return Dialog(
child: FutureBuilder(
future: _loadImage(),
builder: (BuildContext context, AsyncSnapshot<Image> image) {
if (image.hasData) {
return image.data; // image is ready
} else if (image.hasError) {
return Text("No photo available"); // placeholder
} else {
return Text("Impossible");
}
},
));
}
Future<Image> _loadImage() async {
return Image.network(
'http://192.168.1.19:5000/photo',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization':
'Bearer token',
},
key: ValueKey(new Random().nextInt(100)),
);
}
}
Thx in advance :)