Flutter中的错误:RangeError(RangeError(索引):无效值:有效值范围为空:0)

问题描述 投票:0回答:1

我正在 flutter [Dart] 中构建一个电子商务应用程序。 我正在尝试从 firebase 获取数据,其中我有集合名称(供应商集合)。在这个集合中,我有文档和该文档中的一些字段,但是当我尝试访问这些数据时,它返回范围错误。

This is my code where I got error: 
FutureBuilder(
    future: StoreServices.getProfile(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
      if (!snapshot.hasData) {
        return Center(child: loadingIndicator(circleColor: white));
      } else {
        //controller.snapshotData = snapshot.data!.docs[0];
        var data = snapshot.data!.docs[0];
        return Column(
          children: [
            ListTile(
              leading: Image.asset(imgProduct)
                  .box
                  .roundedFull
                  .clip(Clip.antiAlias)
                  .make(),
              title: boldText(text: "${data['vendor_name']}"),
              subtitle: normalText(text: "[email protected]"),
            ),
            const Divider(),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: List.generate(
                    profileButtonIcons.length,
                    (index) => ListTile(
                          onTap: () {
                            switch (index) {
                              case 0:
                                Get.to(() => const ShopSettings());
                                break;
                              case 1:
                                Get.to(() => const MessagesScreen());
                                break;
                              default:
                            }
                          },
                          leading: Icon(
                            profileButtonIcons[index],
                            color: white,
                          ),
                          title:
                              normalText(text: profileButtonTitles[index]),
                        )),
              ),
            )
          ],
        );
      }
    },
  ),
(https://i.stack.imgur.com/dFHcg.png)

这是我创建方法的文件:

class StoreServices {
 static getProfile() {
  return firestore
    .collection(vendorCollection)
    .where('id', isEqualTo: currentUser!.uid)
    .get();
  }
}
(https://i.stack.imgur.com/mg0TA.png)

这是我的 Firebase :(https://i.stack.imgur.com/X4y34.png)

Suggestions will be appreciated. Thanks in advance.

I try to print vendor name and vendor details which has in documents but I got an error. I want to access those data from firebase
flutter firebase dart firebase-realtime-database range
1个回答
0
投票

如果您知道文档 ID,请使用

DocumentSnapshot
获取单个文档:

Future<DocumentSnapshot<Map<String, dynamic>>> getProfile() async {
  try {
    final profile =
        await FirebaseFirestore.instance.collection("name").doc('uid').get();

    if (!profile.exists) {
      throw Exception("Profile not found");
    }

    return profile;
  } catch (e) {
    rethrow;
  }
}

并像这样处理

Future
的所有情况:

FutureBuilder(
      future: futureMethodHere(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.hasError) {
          return Center(child: Text(snapshot.error.toString()));
        } else if (snapshot.hasData) {
          return Text(snapshot.hasData.toString()); 
        } else {
          return const Text('No data available');
        }
      },
    )
© www.soinside.com 2019 - 2024. All rights reserved.