簡體   English   中英

RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore

[英]RangeError (index): Invalid value: Valid value range is empty: 0 in flutter Firestore

將我的項目連接到 Firestore 並創建集合、文檔等后,當我嘗試在索引 0 處打印產品名稱時,這是我遇到的錯誤。

以下 RangeError 被拋出 building FutureBuilder(dirty, state: _FutureBuilderState#18c55): RangeError (index): Invalid value: Valid value range is empty: 0

我已經閱讀了這里發布的類似問題,但它並沒有解決我的問題

以下是我的代碼除外

class Home extends StatelessWidget {

Product menData;

@override
Widget build(BuildContext context) {
  return WillPopScope(
    onWillPop: _onBackPressed,
      child: Scaffold(
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.only(top: 10, left: 10, right: 10),
              child: FutureBuilder(
             future: FirebaseFirestore.instance.collection("products")
                      .doc("6di1OSBgwOjyLbPtl75g").collection("recent").get(),
            builder: (context, snapshot) {
              if(snapshot.connectionState == ConnectionState.waiting){
                return Center(
                  child: spinkit,
                );
              }
              menData = Product(
                image: snapshot.data.documents[0]["image"],
                name: snapshot.data.documents[0]["name"],
                price: snapshot.data.documents[0]["price"],
              );
              print(menData.name);

這是模型

導入 'package:flutter/material.dart';

class Product {
  final String image;
  final String name;
  final double price;

  Product({@required this.image, @required this.name, @required this.price});

}

這是數據庫截圖

FutureBuilder 傾向於隱藏數據類型錯誤。 這很可能是您調用FirebaseFirestore.instance.collection("products").doc("6di1OSBgwOjyLbPtl75g").collection("recent").get(),

我猜有些產品沒有加載正確的類型,

      final String image;
      final String name;
      final double price;  

可能double price是作為int或類似的東西出現的。 我已經診斷出其中一些問題,它們幾乎總是像您的錯誤一樣出現:

使用此示例的“快照視圖檢查”交換您的視圖。 它將驗證您的錯誤並顯示正確的錯誤: https : //api.flutter.dev/flutter/widgets/FutureBuilder-class.html

List<Widget> children;
          if (snapshot.hasData) {
            children = <Widget>[
              Icon(
                Icons.check_circle_outline,
                color: Colors.green,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Result: ${snapshot.data}'),
              )
            ];
          } else if (snapshot.hasError) {
            children = <Widget>[
              Icon(
                Icons.error_outline,
                color: Colors.red,
                size: 60,
              ),
              Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Error: ${snapshot.error}'),
              )
            ];
          } else {
            children = <Widget>[
              SizedBox(
                child: CircularProgressIndicator(),
                width: 60,
                height: 60,
              ),
              const Padding(
                padding: EdgeInsets.only(top: 16),
                child: Text('Awaiting result...'),
              )
            ];
          }
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: children,
            ),
          );

發生此錯誤"Invalid value: Valid value range is empty: 0"是因為您的列表索引值為空,但您必須強制調用該值。

嘗試將這 3 行臨時更改為硬編碼值:

image: snapshot.data.documents[0]["image"]
name: snapshot.data.documents[0]["name"]
price: snapshot.data.documents[0]["price"]

to 

image: 'hardcode'
name: 'harcode'
price: 'hardcode'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM