显示来自 Firebase 的图像

时间:2021-06-29 17:39:04

标签: firebase flutter dart flutter-layout firebase-storage

我正在尝试显示存储在 Firebase 中的图像。我试过这段代码,但它给了我这个错误 主体可能正常完成,导致返回 'null',但返回类型是一个潜在的不可为空的类型。 这个错误来自builder: (context, snapshot){

处的 {

我该如何修复代码?

[编辑]下面的代码没有给出更多错误,但没有显示图像

依赖版本:

  • firebase_core:^1.3.0
  • firebase_analytics:^7.0.1
  • firebase_auth:^1.4.1
  • cloud_firestore:^2.2.2
  • firebase_storage:^8.1.3
InkWell(
    onTap: () {
      Navigator.push(context,MaterialPageRoute(builder: (context) => StantonPlanet())); //Pyro
    },
    child: Container(
      margin: EdgeInsets.only(bottom: 5),
      height: 180,
      child: Card(
        color: blue,
        semanticContainer: true,
        clipBehavior: Clip.antiAliasWithSaveLayer,
        child: FutureBuilder(
          future: _getImage(context, 'pyro.png'),
          builder: (context, snapshot){

              if(snapshot.connectionState == ConnectionState.done){
                return Container(
                 child: snapshot.data,
                );
              }

              if(snapshot.connectionState == ConnectionState.waiting){
               return Container(child: CircularProgressIndicator(),);
              }

              return Container();

          }
        ),
        //Image.asset('assets/images/systems/pyro.png', fit: BoxFit.fill,),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
        elevation: 0,
        margin: EdgeInsets.all(10.0),
      ),
    ),
  ),//close InkWell

这是课程

class FireStorageService extends ChangeNotifier{
  FireStorageService();
  static Future<dynamic> loadImage(BuildContext context, String Image) async{
    return await FirebaseStorage.instance.ref().child(Image).getDownloadURL();
  }
}

这就是未来

 Future<Widget> _getImage(BuildContext context, String imageName) async{
    Image image = '' as Image;
    await FireStorageService.loadImage(context, imageName).then((value) {
     image =Image.network(
       value.toString(),
       fit: BoxFit.fill,
     );
    });
    return image;
  }

1 个答案:

答案 0 :(得分:0)

已修复

InkWell(
    onTap: () {
      Navigator.push(context,MaterialPageRoute(builder: (context) => StantonPlanet())); //Pyro
    },
    child: Container(
      margin: EdgeInsets.only(bottom: 5),
      height: 180,
      child: Card(
        color: blue,
        semanticContainer: true,
        clipBehavior: Clip.antiAliasWithSaveLayer,
        child: FutureBuilder(
          future: _getImage(context, 'pyro.png'),
          builder: (context, snapshot){
              if(snapshot.hasError){
                 return Text('${snapshot.error}');
              }
              if(snapshot.connectionState == ConnectionState.done){
                return Container(
                 child: snapshot.data,
                );
              }

              if(snapshot.connectionState == ConnectionState.waiting){
               return Container(child: CircularProgressIndicator(),);
              }

              return Container();

          }
        ),
        //Image.asset('assets/images/systems/pyro.png', fit: BoxFit.fill,),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0),
        ),
        elevation: 0,
        margin: EdgeInsets.all(10.0),
      ),
    ),
  ),//close InkWell

这是课程

class FireStorageService extends ChangeNotifier{
  FireStorageService();
  static Future<String> loadImage(BuildContext context, String image) async{
    return await FirebaseStorage.instance.ref().child(image).getDownloadURL();
  }
}

这就是未来

 Future<Widget> _getImage(BuildContext context, String imageName) async{
    Image image ;
    final value = await FireStorageService.loadImage(context, imageName);
    image =Image.network(
      value.toString(),
      fit: BoxFit.fill,
    );
    return image;
  }