当前位置: 首页 > 知识库问答 >
问题:

从Firebase存储获取所有图像

邹驰
2023-03-14
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:flutter/material.dart';

class StorageAlbumDisplay extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return StorageAlbumDisplayState();
  }
}

class StorageAlbumDisplayState extends State<StorageAlbumDisplay> {
  String _text;

  @override
  void initState() {
    super.initState();
    getImages();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("ALBUM DİSPLAY PAGE"),
      ),
      body: Container(
        child: Column(
          children: [
            RaisedButton(
                child: Text("Get Images"),
                onPressed: () {
                  getImages();
                }),
            Flexible(
              child: GridView.builder(
                  shrinkWrap: true,
                  itemCount: 10,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2),
                  itemBuilder: (context, index) {
                    return GridTile(
                      child: Center(child: Text(_text),),
                    );
                  }),
            ),
          ],
        ),
      ),
    );
  }

   void getImages() async {
    final firebase_storage.ListResult result = await firebase_storage
        .FirebaseStorage.instance
        .ref()
        .child("deneme")
        .child("resimler")
        .listAll();
    //result.items.forEach((firebase_storage.Reference ref) {
      //debugPrint("found file: ${ref.name}");
    //});

    //debugPrint("resultın ilk elemanı adı: " + result.items[1].name.toString());

    await firebase_storage.FirebaseStorage.instance
        .ref()
        .child("deneme")
        .child("resimler")
        .child(result.items[1].name.toString())
        .getData();
    var url = await firebase_storage.FirebaseStorage.instance
        .ref()
        .child("deneme")
        .child("resimler")
        .child(result.items[1].name.toString())
        .getDownloadURL();

    debugPrint("ilk resim urli: " + url.toString());

    final _text= url.toString();
    setState(() {    });
    debugPrint("text: "+ _text);

    
  }

}


下面是我得到的控制台输出和错误:

Performing hot reload...
Syncing files to device SNE LX1...

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building:
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'

When the exception was thrown, this was the stack: 
#2      new Text (package:flutter/src/widgets/text.dart:370:10)
#3      StorageAlbumDisplayState.build.<anonymous closure> (package:firebase/StorageAlbumDisplay.dart:43:44)
#4      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:449:22)
#5      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1130:28)
#6      SliverMultiBoxAdaptorElement.performRebuild.processElement (package:flutter/src/widgets/sliver.dart:1076:66)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 7 of 663 libraries in 2.441ms.
W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)

下面是我按下Get Images按钮后的控制台输出,您可以看到图像的url为:

W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
W/meb.firebase(24891): Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
V/AudioManager(24891): querySoundEffectsEnabled...
I/flutter (24891): the pic url: https://firebasestorage.googleapis.com/v0/b/kalfa-firebase-projem.appspot.com/o/deneme%2Fresimler%2Fisim1608375656296?alt=media&token=b0ce2662-b668-4832-af20-0e9a20cf5446
I/flutter (24891): text: https://firebasestorage.googleapis.com/v0/b/kalfa-firebase-projem.appspot.com/o/deneme%2Fresimler%2Fisim1608375656296?alt=media&token=b0ce2662-b668-4832-af20-0e9a20cf5446

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building:
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'

When the exception was thrown, this was the stack: 
#2      new Text (package:flutter/src/widgets/text.dart:370:10)
#3      StorageAlbumDisplayState.build.<anonymous closure> (package:firebase/StorageAlbumDisplay.dart:43:44)
#4      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:449:22)
#5      SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1130:28)
#6      SliverMultiBoxAdaptorElement.performRebuild.processElement (package:flutter/src/widgets/sliver.dart:1076:66)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 370 pos 10: 'data != null'
════════════════════════════════════════════════════════════════════════════════════════════════════

共有1个答案

贲绪
2023-03-14

最后,我解决了我的问题,只是得到一个简单的错误,我无法找出原因。正如@asifali和@mukul所说,没有getDowLoadURL我就做不到。在我使用...ListAll()获取存储的映像名称和getDownloadURL之后,然后(...)...将它们放入我的FireBaseFireStore集合中,并使用Image.Network(“urlofimages”)获取所有映像。下面是我的解决方案代码、控制台输出和我得到的错误。如果你能帮我解决这个新的错误,我会很高兴的。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

import 'package:flutter/material.dart';

class StorageAlbumDisplay extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return StorageAlbumDisplayState();
  }
}

class StorageAlbumDisplayState extends State<StorageAlbumDisplay> {


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("ALBUM DİSPLAY PAGE"),
      ),
      body: StreamBuilder(
          stream: FirebaseFirestore.instance.collection("storage_images").snapshots(),
          builder: (context, snapshot) {
            final querySnapshot = snapshot.data;
            return Container(
              child: Column(
                children: [
                  RaisedButton(
                      child: Text("Get Images"),
                      onPressed: () {
                        getImages();
                      }),
                  Flexible(
                    child: GridView.builder(
                        shrinkWrap: true,
                        itemCount: 10,
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 2),
                        itemBuilder: (context, index) {
                          final map = querySnapshot.docs[index].data();
                          return GridTile(
                            child: Image.network(map["url"]),
                          );
                        }),
                  ),
                ],
              ),
            );
          }
      ),
    );
  }

  void getImages() async {
    for(int i=0; i<10; i++){

      final _resultName  = await firebase_storage
          .FirebaseStorage.instance
          .ref()
          .child("deneme")
          .child("resimler")
          .listAll().then((value) => value.items[i].name);
      String resultName = _resultName.toString();
      debugPrint("resultın ${i+1}. elemanının adı: " + resultName);
      
      final _resultUrl  = await firebase_storage
        .FirebaseStorage.instance
        .ref()
        .child("deneme")
        .child("resimler")
        .listAll().then((value) => value.items[i].getDownloadURL());
      String resultUrl = _resultUrl.toString();
      debugPrint("resultın ${i+1}. elemanının urli: " + resultUrl);

      await FirebaseFirestore.instance.collection("storage_images").add({
        "name": resultName, "url": resultUrl
      });
    }


  }
}

控制台输出和错误:

Performing hot restart...
Syncing files to device SNE LX1...
Restarted application in 2.856ms.
W/meb.firebase(10713): Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
V/AudioManager(10713): querySoundEffectsEnabled...
W/DynamiteModule(10713): Local module descriptor class for providerinstaller not found.
D/ConnectivityManager(10713): requestNetwork and the calling app is: meb.firebase
I/DynamiteModule(10713): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(10713): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
D/ConnectivityManager(10713): requestNetwork and the calling app is: meb.firebase
V/AudioManager(10713): querySoundEffectsEnabled...
W/meb.firebase(10713): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/meb.firebase(10713): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building StreamBuilder<QuerySnapshot>(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot<QuerySnapshot>>#eb94f):
The getter 'size' was called on null.
Receiver: null
Tried calling: size

The relevant error-causing widget was: 
  StreamBuilder<QuerySnapshot> file:///C:/ornekler/firebase/flutter_app/firebase/lib/StorageAlbumDisplay.dart:23:13
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      StorageAlbumDisplayState.build.<anonymous closure> (package:firebase/StorageAlbumDisplay.dart:38:50)
#2      StreamBuilder.build (package:flutter/src/widgets/async.dart:525:81)
#3      _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:129:48)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
...
════════════════════════════════════════════════════════════════════════════════════════════════════

还有一件事:如果我想用FutureBuilder而不是Streambuilder来做这件事,我会得到一个future错误。如果有谁做这与未来建设者,我也将是伟大的。谢谢你的帮助。

 类似资料:
  • 有没有办法从Firebase存储中获取所有的URL? 我知道有从firebase数据库获取URL的方法,比如将firebase存储文件的URL存储在firebase实时数据库中,然后从它获取URL。但是我正在从图片中显示的按钮上传图片,并且想要所有那些上传图片的URL直接从firebase存储路径像... 用它就像... 如有任何帮助,我们将不胜感激。

  • 所以,我想让用户同时上传多个图像到firebase存储。我已经成功地完成了这部分,但现在我想要取回图像。一开始,我上传一个图像给每个用户,并将图像id保存到实时数据库中,这样就很容易通过图像id检索回图像。但是现在我如何为每个用户保存多个图像到实时数据库我不能使用相同的子名,因为它将被替换为旧的。有什么想法吗?? 上载图像并在实时数据库上设置id: 我的firebase数据库:

  • 我要上传一张图片到firebase的存储器。我用的不是网页版存储(不应该用)。我正在使用firebase admin。没问题,我毫无困难地上传了文件,并在变量“file”中得到结果。 如果我访问firebase存储控制台,图像就在那里。好吧 现在,我有一些问题。 > < li> 为什么对< code>upload ()方法的响应有这么多信息和这么多对象?回顾这个巨大的对象,我在元数据中发现了一个名

  • 我想从FireBase的方法获取下载Url。如何使用以下代码获取下载Url? 我使用了,但这不起作用。

  • 我试图使用查询特定的产品类型和大小(两个diff表中的列),从以下所有表中获取数据<我无法得到想要的结果,所以一些指导将非常有用。 请在下面找到我迄今为止尝试过的细节。 PK:主键,FK:外键 以下是表结构,下面的表(提到)映射到另一个表。 实体类: 存储库: 我使用了以下查询(一个接一个),基于prod_类型和大小,我需要从所有表中获取数据。 注:尺寸不是表MC\U产品类型的一部分。数据团队已告