我正在尝试从Firebase存储桶中获取图像(正在运行),但似乎试图在通过异步接收到图像之前加载该图像。似乎在设置图片网址之前已分配findScreen。
我完全无法描述这个问题,所以我希望代码示例能更好地说明问题。
// This function is getting the image and returning it.
// It is outside of the class structure
// In file called find_screen.dart and imported in main.dart
Future<String> _getMainBg() async {
final FirebaseApp app = await FirebaseApp.configure(
name: 'test',
options: FirebaseOptions(
googleAppID:'appID',
apiKey: 'APIKEY',
projectID: 'sample_project',
),
);
try {
final FirebaseStorage storage = FirebaseStorage(
app: app, storageBucket: 'gs://storagebucket.appspot.com');
var ref = storage.ref().child('main_bg.jpg');
String url = (await ref.getDownloadURL()).toString();
return url;
} on PlatformException {
return "some error";
}
}
// This is a variable holding the Screen object.
// This is also outside of the class scope.
// In file called find_screen.dart and imported in main.dart
final findScreen = new Screen(
title: 'Some Title',
background: new DecorationImage(
image: new NetworkImage(_getMainBg.toString()),
fit: BoxFit.cover,
),
contentBuilder: (BuildContext context) {.......});
// This is called from the main.dart file which sets activeScreen as findScreen
case "find":
setState(() => activeScreen = findScreen);
break;
我希望函数_getMainBg返回存储桶中图像的URL并分配给图像:new NetworkImage(),然后将其传递给activeScreen。