a-marenkov / gsheets

A Dart library for working with Google Sheets API.
Other
80 stars 31 forks source link

Error on AllRow method #34

Closed FarsFuad closed 3 years ago

FarsFuad commented 3 years ago

When I use allrow method to get them all and to store them , I keep geting an error if the last colume of the sheet has empty cells

for ex :

Screenshot 2020-12-19 111956

It works if the least colum is full , but doesn't work if its not full..

a-marenkov commented 3 years ago

Hi @FarsFuad

I've tried to reproduce the error with your example, but it worked as expected. I've checked print(await sheet.values.allRows());

Can you please share a sample of the code that fails?

Also if you provide error message/stack trace it might help to figure out where is the error.

Thanks!

FarsFuad commented 3 years ago

E/flutter ( 6393): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: RangeError (index): Invalid value: Not in inclusive range 0..42: 43 E/flutter ( 6393): #0 List.[] (dart:core-patch/growable_array.dart:177:60) E/flutter ( 6393): #1 _SearchState.refreshButton (package:googlesheet/Screens/AdminScreen/Search.dart:137:27) E/flutter ( 6393): E/flutter ( 6393): #2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19) E/flutter ( 6393): #3 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:1111:38) E/flutter ( 6393): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24) E/flutter ( 6393): #5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11) E/flutter ( 6393): #6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5) E/flutter ( 6393): #7 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7) E/flutter ( 6393): #8 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:476:9) E/flutter ( 6393): #9 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:77:12) E/flutter ( 6393): #10 PointerRouter._dispatchEventToRoutes. (package:flutter/src/gestures/pointer_router.dart:122:9) E/flutter ( 6393): #11 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8) E/flutter ( 6393): #12 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:120:18) E/flutter ( 6393): #13 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:106:7) E/flutter ( 6393): #14 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:358:19) E/flutter ( 6393): #15 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:338:22) E/flutter ( 6393): #16 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:267:11) E/flutter ( 6393): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:295:7) E/flutter ( 6393): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:240:7) E/flutter ( 6393): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:213:7) E/flutter ( 6393): #20 _rootRunUnary (dart:async/zone.dart:1206:13) E/flutter ( 6393): #21 _CustomZone.runUnary (dart:async/zone.dart:1100:19) E/flutter ( 6393): #22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7) E/flutter ( 6393): #23 _invoke1 (dart:ui/hooks.dart:265:10) E/flutter ( 6393): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5) E/flutter ( 6393):

this is the error , It says that the rang of colums are 43 , but acculy they are 44

becuse the last colume has an emtpy cells

FarsFuad commented 3 years ago

try to get all rows and store them in datatype for ex :

var activeOrder =await gSheet.sheet.values.allRows();

List<detile> order = List<detile>(activeOrder.length);

for (int i = activeOrder.length-1; i > 0; i--) {

  List orderColum = activeOrder[i];

  if (orderColum.isNotEmpty) {
    order[i] = detile(
      data1: orderColum[0],
      data2: orderColum[1],
       data3: orderColum[2],
      data4: orderColum[3],
      data5: orderColum[4],
      data6: orderColum[5],
      data7: orderColum[6],
      data8: orderColum[7],  
      ....
      ....
      ....
      ....
      ....
      data44: orderColum[43],  
    );
FarsFuad commented 3 years ago

you will get an error if the last colume has a cell that got no value

a-marenkov commented 3 years ago

Ok, i see the problem now.

Some rows are shorter in this case.

I thought that it's better this way, so it's a normal behavior, but i think i should consider adding empty cells.

I'll think about it and get back to you.

Meanwhile, you can temporarily fix this in your project, like so.

  1. Add this extension to your file.
    extension ListX<T> on List<T> {
    /// Safetly gets value by [index]
    /// 
    /// If [index] out of bounds returns [fallback]
    T tryGet(int index, {T fallback}) {
    if (index < 0 || index > length - 1) {
      return fallback;
    }
    return this[index];
    }
    }
  2. Use added extension
    
      data44: orderColum.tryGet(43), 

or

  data44: orderColum.tryGet(43, fallback: ''), // with any fallbak you like  
a-marenkov commented 3 years ago

Hi @FarsFuad

I have fixed it in 0.3.0-dev.1

It's currently in pre-release, but you can use it.