วิธีแสดงไฟล์รูปภาพใน firestore ด้วย Flutter

ฉันพบปัญหาต่อไปนี้ แล้วฉันก็เข้าใจมัน

Flutter / FireStore: วิธีแสดงรูปภาพจาก Firestore ใน Flutter?

การอัพโหลดไฟล์สำเร็จ

var imgUrl = await ref.getDownloadURL();
print(imgUrl.toString());

อย่างไรก็ตามฉันมีข้อผิดพลาดดังต่อไปนี้ ดูเหมือนว่าฉันก็กำลังทำเหมือนกัน

ข้อยกเว้นที่ไม่สามารถจัดการได้: PlatformException (ข้อผิดพลาด -13010, FIRStorageErrorDomain, วัตถุรูปภาพ/cars/40711b90-9db4-11ea-c602-a557c9b7697a.jpeg ไม่มีอยู่)

อย่างไรก็ตาม ฉันไม่รู้ว่าจะแสดงและจัดการมันอย่างไร

โปรดให้คำแนะนำแก่ฉัน ขอบคุณ.


person tajihiro    schedule 24.05.2020    source แหล่งที่มา
comment
คุณมีที่เก็บรูปภาพใน FireBaseStorage และรูปภาพนั้นมีกฎที่เหมาะสมในการเข้าถึงหรือไม่   -  person Jitesh Mohite    schedule 24.05.2020
comment
ฉันกำลังลองใช้แอปพลิเคชันเดียวกัน กฎเกณฑ์ควรจะเหมือนกัน rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } }   -  person tajihiro    schedule 24.05.2020
comment
ป้อนเส้นทางรูปภาพเบราว์เซอร์ของคุณและตรวจสอบว่ารูปภาพแสดงหรือไม่ หากแสดงทุกอย่างถูกต้อง   -  person Jitesh Mohite    schedule 24.05.2020


คำตอบ (1)


คุณต้องเพิ่ม url ไปที่ firestore ก่อน:

          StorageTaskSnapshot snapshot = await storage
              .ref()
              .child("images/$imageName")
              .putFile(file)
              .onComplete;
          if (snapshot.error == null) {
            final String downloadUrl =
                await snapshot.ref.getDownloadURL();
            await Firestore.instance
                .collection("images")
                .add({"url": downloadUrl, "name": imageName});
             }

ตอนนี้ใน Firestore คุณจะมีคอลเลกชันชื่อ images และเอกสารพร้อม URL รูปภาพและชื่อรูปภาพ เมธอด getDownloadUrl() ส่งคืน URL ของรูปภาพเพื่อให้คุณสามารถจัดเก็บไว้ใน Firestore ได้ จากนั้นหากต้องการแสดง คุณสามารถทำสิ่งต่อไปนี้:

body: Container(
  padding: EdgeInsets.all(10.0),
  child: FutureBuilder(
    future: getImages(),
    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        return ListView.builder(
            shrinkWrap: true,
            itemCount: snapshot.data.documents.length,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                contentPadding: EdgeInsets.all(8.0),
                title:
                    Text(snapshot.data.documents[index].data["name"]),
                leading: Image.network(
                    snapshot.data.documents[index].data["url"],
                    fit: BoxFit.fill),
              );
            });
      } else if (snapshot.connectionState == ConnectionState.none) {
        return Text("No data");
      }
      return CircularProgressIndicator();
    },
  ),
),

/// code here

  Future<QuerySnapshot> getImages() {
    return fb.collection("images").getDocuments();
  }

ที่นี่คุณใช้วิธีการ getImages() ซึ่งจะดึงภาพทั้งหมดจากคอลเลกชัน images ในการแสดงภาพคุณสามารถใช้วิดเจ็ต Image.network

person Peter Haddad    schedule 24.05.2020
comment
ฉันอาจใช้การออกแบบที่ไม่ถูกต้องในการจัดเก็บข้อมูลใน firestore ฉันไม่มีความคิดที่ดีที่จะระบุรูปภาพ ฉันจะเก็บข้อมูล url ใน firestore ได้อย่างไร - person tajihiro; 24.05.2020
comment
ฉันไม่เข้าใจว่าคุณหมายถึงอะไร? - person Peter Haddad; 24.05.2020
comment
ฉันต้องการอัปโหลดภาพลงใน firestrage จากนั้นดึงภาพมาแสดงเร็วๆ นี้ อย่างไรก็ตาม ตอนนี้ฉันมี http URL ใน firedatabase - person tajihiro; 24.05.2020
comment
โค้ดด้านบนทำสิ่งที่คุณต้องการทุกประการ putFile กำลังเพิ่มรูปภาพในที่เก็บข้อมูล firebase add() กำลังสร้างเอกสารพร้อม URL ของรูปภาพในที่เก็บข้อมูล firebase ในส่วนที่สองของโค้ด รูปภาพจะถูกดึงออกมาและแสดงบนหน้าจอ - person Peter Haddad; 24.05.2020
comment
รหัสมาจากบทช่วยสอนนี้ petercoding.com/firebase /2020/05/06/ , ซอร์สโค้ด: github.com/PeterHdd/Firebase -การจัดเก็บ-บทช่วยสอน - person Peter Haddad; 24.05.2020
comment
ฉันกำลังหลงทาง ถ้าฉันมีข้อมูล URL เป็น Image.network( snapshot.data.documents[index].data["url"], เป็น https://xxxx URL ปกติ มันอาจจะผิดวิธี - person tajihiro; 24.05.2020
comment
ฉันจะเก็บข้อมูล URL ได้อย่างไร? - person tajihiro; 24.05.2020
comment
ให้เราสนทนาต่อในการแชท - person Peter Haddad; 24.05.2020