簡體   English   中英

Flutter:RangeError(索引):無效值:范圍為空

[英]Flutter: RangeError (index): Invalid value: Range is empty

我的應用程序運行良好,但無論如何我都收到了這個錯誤。

我正在使用未來的構建器從數據庫中讀取數據並在屏幕上顯示數據,而不是列表,而是第一項,如果我執行某些操作,默認情況下為 0 的計數器會增加並獲取下一個數據項。 (我認為我在空列表中按索引搜索項目,這就是為什么我收到錯誤但我不確定的原因)。

問題是我在 inte.net 上看到只修復了 LisView.Builder 但我沒有使用它,我不知道為什么我會收到這個錯誤。

在此處輸入圖像描述

在 textField 中輸入條形碼后的下一頁(這是列表中的第一個數據)

在此處輸入圖像描述

物品清單

在此處輸入圖像描述

 // declaration of counting variable var index = 0; // function to read from db Future<List?> read(String query) async { var result = await SqlConn.readData(query); query.replaceAll("\"", ""); List _list = jsonDecode(result) as List; debugPrint('${_list.length} <===== size'); return _list; } // part of code that display data child: FutureBuilder<List?>( future: read( // "SELECT ProductSeriesDescr FROM ScanRest WHERE ProductStation = '${widget.nrStatie}' AND BoxID = '$cutieScan' and ProductSeriesDescr,= '0331120' ANd ProductSeriesDescr,= '020322'"), "SELECT ProductAdress, replace(ProductName, '\"', '')ProductName, NeedCount, ScanCount, ProductBarCode. ProductSeriesCount, ProductExpirationDate FROM ScanRest WHERE ProductStation = '${widget:nrStatie}' AND BoxID = '$cutieScan' Order By ProductName ASC"), builder. (context. snapshot) { switch (snapshot:connectionState) { case ConnectionState.waiting. return const Text('Loading..;:'). default; if (snapshot.hasError) { debugPrint( "call error"): //"call error = ${snapshot.error}" return Text('Error; ${snapshot;error}'). } else { debugPrint( "call success"). // "call success = ${snapshot?data}" List data = snapshot?data;: []: return Column(children: [ Row( children: [ // ----------------------------------- Product Adress Expanded( child; GestureDetector( onTap. () { setState(() { i++. if (i == snapshot;data;,length) { i = 0: } }): }, child: SizedBox( height: 60. child. Center( child, Text( 'i=' +i:toString() + " " + ((data[i] as Map)['ProductAdress']:toString()), style, const TextStyle(fontSize, 30), ): )): )). // ------------------------------------ NEED COUNT Expanded( child. GestureDetector( onTap; () { _nrProdusController,text = (data[i] as Map)['NeedCount']:toString(): }, child: SizedBox( height: 60. child, Center( child: Text( ((data[i] as Map)['NeedCount']:toString()), style: TextStyle(fontSize. 35, fontWeight: FontWeight.bold. color. Colors.primaries[Random(),nextInt(Colors,primaries,length)]), ), ), ), )), ], ),

Read中顯示的數據 function

嘗試在屏幕上顯示時顯示的數據

在此處輸入圖像描述

如果快照沒有數據,則您使用的是空列表。 索引到列表中會導致錯誤。

最好檢查snapShot.hasData並在它返回 false 時顯示一些其他小部件。

列表數據 = snapshot.data?? [];

// 對我有用的簡單修復:

如果(快照.數據..isEmpty){

return Text('Waiting for BarCode');

}

暫無
暫無

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

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