簡體   English   中英

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

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

我的應用程序運行良好,沒有問題。

我有: var i (as index) = 0我分配給列表中的第一個數據項,該列表目前為空,所以這就是出現錯誤的原因。

我要么需要以某種方式隱藏錯誤,要么需要一種修復它的方法。

在此處輸入圖像描述

 // 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)]), ), ), ), )), ], ),

讀取功能中顯示的數據

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

在此處輸入圖像描述

請使用snapshot.hasData()來確保你有數據。 你可以參考這個flutter 文檔。 我可以給出非常基本的示例:

FutureBuilder(
  builder: (ctx, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
          //have an error
      } else if (snapshot.hasData) {
          //sure there is data
      }
    } 
  ),

暫無
暫無

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

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