I'm trying to get url from my Firebase inform of a string and i want the process to wait for the http request to complete before proceeding because this url is to be used as the image url.
I want to make an app that automatically takes an image from a server and displays it automatically.Someone please help
I have tried future builder but i get the above error
class _MyScreensHomeState extends State<MyScreensHome> {
Future<String> _getmorningScreen1() async {
var response = await http.get(
'https://xxxxx.firebaseio.com/Shows/Morning_Show/Screen1.json');
final String latestResponse = json.decode(response.body);
return latestResponse;
}
Widget _defaultImage() {
return Image.asset(
'assets/Kiss_default.jpg',
fit: BoxFit.fill,
height: double.infinity,
width: double.infinity,
);
}
Widget _connectingImage() {
return Image.asset(
'assets/Kiss_default.jpg',
fit: BoxFit.fill,
height: double.infinity,
width: double.infinity,
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('Screen 2'),
),
body: FutureBuilder(
future: _getmorningScreen1(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
_defaultImage();
break;
case ConnectionState.waiting:
_connectingImage();
break;
case ConnectionState.done:
if (snapshot.hasError) {
return Text('Error:${snapshot.error}');
} else {
return Image.asset(
(snapshot.data).toString(),
fit: BoxFit.fill,
height: double.infinity,
width: double.infinity,
);
}
break;
default:
Card(
child: Center(
child: Text('No Images to Display'),
),
);
}
return Container(width: 0.0, height: 0.0);
},
),
),
);
}
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: https://i.postimg.cc/NjZGz6fS/Studio-Screens-Wake-01.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:221
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:464
<asynchronous suspension>
#2 AssetBundleImageProvider.load
package:flutter/…/painting/image_provider.dart:449
...
Image provider: AssetImage(bundle: null, name: "https://i.postimg.cc/NjZGz6fS/Studio-Screens-Wake-01.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#966e8(), name: "https://i.postimg.cc/NjZGz6fS/Studio-Screens-Wake-01.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════
FutureBuilder<String>
, make it plainFutureBuilder
.Future
method inside.