0

I have an app which shows data that comes from a API, but I'm having a problem, when the api isnt loaded yet the app is showing a error (because the list is empty and there is no data to show).

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

Here is how i initiate the List at the top of the class:

List produtosList1 = <ProdutoOs>[];

Here is how i show the data:

  child: Text(
                           
            produtosList1[index].data,
            style: TextStyle(fontWeight: FontWeight.bold),
                          ),

Is there a way to change the "produtosList1[index].data" to a specific string when the List is empty?

2 Answers 2

1
child: Text(                  
 produtosList1.isEmpty()?"data is empty":produtosList1[index].data,
 style: TextStyle(fontWeight: FontWeight.bold),
),
Sign up to request clarification or add additional context in comments.

1 Comment

It worked, its the way i thought but i couldnt do, thanks!
1

You can solve this problem using FutureBuilder(). The step by step procedure would be:

  1. You should write the entire code of fetching data from API into a future method.
  2. pass that future method as an argument in futurebuilder widget.
  3. By using snapshot.hasData condition, check whether information is loaded or not.
  4. If it's loaded, display it in list.
  5. If not, show CircularProgressIndicator(),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.