feMoraes0/quiz-app

When there is no data. I am getting this error, please help to solve this.

Closed this issue · 1 comments

I am using the ternary operator but it is throwing an error. please help me. I want to show No quiz data when there is no data available.

Capturem

E/flutter ( 7649): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 7649): #0 List.[] (dart:core-patch/growable_array.dart:166:60)
E/flutter ( 7649): #1 _QuizState.getQuestions (package:myquiz/screens/quiz.dart:48:33)
E/flutter ( 7649):
E/flutter ( 7649): #2 _QuizState.initState (package:myquiz/screens/quiz.dart:33:10)
E/flutter ( 7649): #3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4684:58)
E/flutter ( 7649): #4 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
E/flutter ( 7649): #5 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #6 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #7 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
E/flutter ( 7649): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 7649): #11 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 7649): #12 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
E/flutter ( 7649): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
E/flutter ( 7649): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #15 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #16 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
E/flutter ( 7649): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #19 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
E/flutter ( 7649): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 7649): #23 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
E/flutter ( 7649): #24 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 7649): #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
E/flutter ( 7649): #26 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4710:11)
E/flutter ( 7649): #27 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
E/flutter ( 7649): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #29 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #30 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
E/flutter ( 7649): #31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #33 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
E/flutter ( 7649): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 7649): #37 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
E/flutter ( 7649): #38 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 7649): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
E/flutter ( 7649): #40 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4710:11)
E/flutter ( 7649): #41 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
E/flutter ( 7649): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
E/flutter ( 7649): #43 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
E/flutter ( 7649): #44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
E/flutter ( 7649): #45 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
E/flutter ( 7649): #46

Hello @PramodBhai-PK, checking length property documentation (https://api.dart.dev/stable/2.12.4/dart-core/List/length.html) you will find the return type as int, so you couldn't check with null. In my original code I had already checked if there are questions in my list to render the component. So, if you want to check if the list is empty, you should check if questions.length is equal zero (do it after a check if questions already exists otherwise you will receive another error).