1

I have a problem when loading data from a FutureBuilder . I put a breakpoint and despite of my global list is coming full of data, the list is still not showing up and it keeps loading with the circular indicator. I do not understand because, in fact, snapshot HAS DATA:

class BookPageBody extends StatefulWidget {
  List<dynamic> breedlist;
  BookPageBody({Key? key, required this.breedlist}) : super(key: key);

  @override
  _BookPageBodyState createState() => _BookPageBodyState();
}

class _BookPageBodyState extends State<BookPageBody> {
  //pagecontroller hace que sea visible el anterior y el posterior slide en la pantalla
  Future<List<dynamic>> ? futureData;

  @override
  void initState() {
    super.initState();
    futureData = fetchData(AppConstants.APIBASE_URL);
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(children: [
        SizedBox(height: Dimensions.height20),
        Container(
            margin: EdgeInsets.only(left: Dimensions.width20),
            child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  BigText(text: 'Breed List'),
                ])),
        FutureBuilder(
          future: futureData,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                  itemBuilder: (BuildContext context, int index) {
                return ListCard(index: index, doglist: widget.breedlist);
              });
            } else if (snapshot.hasError) {
              return Text("${snapshot.error}");
            }
            return CircularProgressIndicator();
          },
        ),
      ]),
    );
  }
}

the method where I call my api and I fill my list:

List<dynamic> BreedList = [];

Future<List<dynamic>> fetchData(url) async {
  var client = http.Client();
  final response = await client.get(Uri.parse(url));
  await Future.delayed(Duration(seconds:2));
  if (response.statusCode == 200) {
    var jsonDecoded = json.decode(response.body);
    BreedList = jsonDecoded.map((data) => DogClass.fromJson(data)).toList();
    glossarList = BreedList;
    return BreedList;
  } else {
    throw Exception('Failed to load data');
  }
}

2 Answers 2

1

As I checked your code. I got to know that may be your global list is not updating data. Means you are getting data inside your global list but its not updating on all screens.

So, In my opinion try to use Provider of any Other state management library for this.

For Provider you can make Provider variable and use that inside the future builder.

Sign up to request clarification or add additional context in comments.

Comments

0

Your ListView.builder has no connection to your actual list. It will always be empty. You need to pass the list of data you get to this builder.

2 Comments

I do pass that list through parameter in another widget : widget.breedlist
But your listview builder will build zero items. It doesn't matter what an item looks like, if there is zero of them...

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.