StackOverflowError was thrown building FutureBuilder<List>










1















I am consuming the New York Times Best Sellers API, so that I can get the list of books for each category in the Best Sellers. My code goes thus:



import "dart:convert";
import "package:flutter/material.dart";
import "package:http/http.dart" as http;
import "package:best_sellers/api.dart";

class BestSellersList extends StatefulWidget
final API api;
final List<dynamic> categories;

BestSellersList(Key key, @required this.api, @required this.categories) : super(key: key);

@override
_BestSellersList createState() => _BestSellersList();


class _BestSellersList extends State<BestSellersList>
String base, key;
List<dynamic> categories;
Future<List<dynamic>> books;

Future<List<dynamic>> fetchBooks() async
try
final books = categories.map((category) async
final listNameEncoded = category["list_name_encoded"];
final uri = "$base/$listNameEncoded?api-key=$key";
final response = await http.get(uri);
if (response.statusCode == 200)
final responseBody = json.decode(response.body);
final books = responseBody["results"]["books"];
return books;
else throw Exception();
).toList();

if (books.isNotEmpty)
return books;

throw Exception();
catch(e) /**/


void updateBooks()
setState(()
books = updateBooks();
);


@override
initState()
super.initState();
base = widget.api.base;
key = widget.api.key;
categories = widget.categories;
print("categories.length: $categories.length"); // categories.length: 55
updateBooks();


@override
Widget build(BuildContext context)
return FutureBuilder(
future: books,
builder: (context, snapshot)
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index)
final displayName = snapshot.data[index]["display_name"];
return ListTile(title: Text(displayName));

);
else if (snapshot.hasError)
return Center(child: Column(
children: [
Text(snapshot.error.toString()),
RaisedButton(
onPressed: () => updateBooks(),
child: Text("RETRY"),
),
],
));


return Center(child: CircularProgressIndicator());

);




On running my code, I get this stack trace:



 I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
...................................................
...................................................
...................................................
I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)


I understand that the bug is located in the fetchBooks() method, which in turn is invoked by the updateBooks() method, and that a StackoverflowError is caused by an excessively deep or infinite recursion. The categories list I'm iterating over has 55 items.










share|improve this question






















  • your updateBooks call itself

    – Rémi Rousselet
    Nov 13 '18 at 16:20











  • @Developal I'm quite sure that the edit you suggested to my answer won't work because fetchBooks() returns a Future<...>.

    – Günter Zöchbauer
    Nov 13 '18 at 16:53
















1















I am consuming the New York Times Best Sellers API, so that I can get the list of books for each category in the Best Sellers. My code goes thus:



import "dart:convert";
import "package:flutter/material.dart";
import "package:http/http.dart" as http;
import "package:best_sellers/api.dart";

class BestSellersList extends StatefulWidget
final API api;
final List<dynamic> categories;

BestSellersList(Key key, @required this.api, @required this.categories) : super(key: key);

@override
_BestSellersList createState() => _BestSellersList();


class _BestSellersList extends State<BestSellersList>
String base, key;
List<dynamic> categories;
Future<List<dynamic>> books;

Future<List<dynamic>> fetchBooks() async
try
final books = categories.map((category) async
final listNameEncoded = category["list_name_encoded"];
final uri = "$base/$listNameEncoded?api-key=$key";
final response = await http.get(uri);
if (response.statusCode == 200)
final responseBody = json.decode(response.body);
final books = responseBody["results"]["books"];
return books;
else throw Exception();
).toList();

if (books.isNotEmpty)
return books;

throw Exception();
catch(e) /**/


void updateBooks()
setState(()
books = updateBooks();
);


@override
initState()
super.initState();
base = widget.api.base;
key = widget.api.key;
categories = widget.categories;
print("categories.length: $categories.length"); // categories.length: 55
updateBooks();


@override
Widget build(BuildContext context)
return FutureBuilder(
future: books,
builder: (context, snapshot)
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index)
final displayName = snapshot.data[index]["display_name"];
return ListTile(title: Text(displayName));

);
else if (snapshot.hasError)
return Center(child: Column(
children: [
Text(snapshot.error.toString()),
RaisedButton(
onPressed: () => updateBooks(),
child: Text("RETRY"),
),
],
));


return Center(child: CircularProgressIndicator());

);




On running my code, I get this stack trace:



 I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
...................................................
...................................................
...................................................
I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)


I understand that the bug is located in the fetchBooks() method, which in turn is invoked by the updateBooks() method, and that a StackoverflowError is caused by an excessively deep or infinite recursion. The categories list I'm iterating over has 55 items.










share|improve this question






















  • your updateBooks call itself

    – Rémi Rousselet
    Nov 13 '18 at 16:20











  • @Developal I'm quite sure that the edit you suggested to my answer won't work because fetchBooks() returns a Future<...>.

    – Günter Zöchbauer
    Nov 13 '18 at 16:53














1












1








1








I am consuming the New York Times Best Sellers API, so that I can get the list of books for each category in the Best Sellers. My code goes thus:



import "dart:convert";
import "package:flutter/material.dart";
import "package:http/http.dart" as http;
import "package:best_sellers/api.dart";

class BestSellersList extends StatefulWidget
final API api;
final List<dynamic> categories;

BestSellersList(Key key, @required this.api, @required this.categories) : super(key: key);

@override
_BestSellersList createState() => _BestSellersList();


class _BestSellersList extends State<BestSellersList>
String base, key;
List<dynamic> categories;
Future<List<dynamic>> books;

Future<List<dynamic>> fetchBooks() async
try
final books = categories.map((category) async
final listNameEncoded = category["list_name_encoded"];
final uri = "$base/$listNameEncoded?api-key=$key";
final response = await http.get(uri);
if (response.statusCode == 200)
final responseBody = json.decode(response.body);
final books = responseBody["results"]["books"];
return books;
else throw Exception();
).toList();

if (books.isNotEmpty)
return books;

throw Exception();
catch(e) /**/


void updateBooks()
setState(()
books = updateBooks();
);


@override
initState()
super.initState();
base = widget.api.base;
key = widget.api.key;
categories = widget.categories;
print("categories.length: $categories.length"); // categories.length: 55
updateBooks();


@override
Widget build(BuildContext context)
return FutureBuilder(
future: books,
builder: (context, snapshot)
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index)
final displayName = snapshot.data[index]["display_name"];
return ListTile(title: Text(displayName));

);
else if (snapshot.hasError)
return Center(child: Column(
children: [
Text(snapshot.error.toString()),
RaisedButton(
onPressed: () => updateBooks(),
child: Text("RETRY"),
),
],
));


return Center(child: CircularProgressIndicator());

);




On running my code, I get this stack trace:



 I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
...................................................
...................................................
...................................................
I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)


I understand that the bug is located in the fetchBooks() method, which in turn is invoked by the updateBooks() method, and that a StackoverflowError is caused by an excessively deep or infinite recursion. The categories list I'm iterating over has 55 items.










share|improve this question














I am consuming the New York Times Best Sellers API, so that I can get the list of books for each category in the Best Sellers. My code goes thus:



import "dart:convert";
import "package:flutter/material.dart";
import "package:http/http.dart" as http;
import "package:best_sellers/api.dart";

class BestSellersList extends StatefulWidget
final API api;
final List<dynamic> categories;

BestSellersList(Key key, @required this.api, @required this.categories) : super(key: key);

@override
_BestSellersList createState() => _BestSellersList();


class _BestSellersList extends State<BestSellersList>
String base, key;
List<dynamic> categories;
Future<List<dynamic>> books;

Future<List<dynamic>> fetchBooks() async
try
final books = categories.map((category) async
final listNameEncoded = category["list_name_encoded"];
final uri = "$base/$listNameEncoded?api-key=$key";
final response = await http.get(uri);
if (response.statusCode == 200)
final responseBody = json.decode(response.body);
final books = responseBody["results"]["books"];
return books;
else throw Exception();
).toList();

if (books.isNotEmpty)
return books;

throw Exception();
catch(e) /**/


void updateBooks()
setState(()
books = updateBooks();
);


@override
initState()
super.initState();
base = widget.api.base;
key = widget.api.key;
categories = widget.categories;
print("categories.length: $categories.length"); // categories.length: 55
updateBooks();


@override
Widget build(BuildContext context)
return FutureBuilder(
future: books,
builder: (context, snapshot)
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index)
final displayName = snapshot.data[index]["display_name"];
return ListTile(title: Text(displayName));

);
else if (snapshot.hasError)
return Center(child: Column(
children: [
Text(snapshot.error.toString()),
RaisedButton(
onPressed: () => updateBooks(),
child: Text("RETRY"),
),
],
));


return Center(child: CircularProgressIndicator());

);




On running my code, I get this stack trace:



 I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
...................................................
...................................................
...................................................
I/flutter ( 5231): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
I/flutter ( 5231): The following StackOverflowError was thrown building FutureBuilder<List<dynamic>>(state:
I/flutter ( 5231): _FutureBuilderState<List<dynamic>>#a3252):
I/flutter ( 5231): Stack Overflow
I/flutter ( 5231): When the exception was thrown, this was the stack:
I/flutter ( 5231): #0 State.mounted (package:flutter/src/widgets/framework.dart:969:3)
I/flutter ( 5231): #1 State.setState.<anonymous closure>
(package:flutter/src/widgets/framework.dart:1113:63)
I/flutter ( 5231): #2 State.setState (package:flutter/src/widgets/framework.dart:1123:6)
I/flutter ( 5231): #3 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #4 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #5 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #6 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #7 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #8 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #9 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #10 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)
I/flutter ( 5231): #11 State.setState (package:flutter/src/widgets/framework.dart:1124:30)
I/flutter ( 5231): #12 _BestSellersList.updateBooks (package:best_sellers/best_sellers_list.dart:44:9)
I/flutter ( 5231): #13 _BestSellersList.updateBooks.<anonymous closure>
(package:best_sellers/best_sellers_list.dart:45:19)


I understand that the bug is located in the fetchBooks() method, which in turn is invoked by the updateBooks() method, and that a StackoverflowError is caused by an excessively deep or infinite recursion. The categories list I'm iterating over has 55 items.







dart flutter future






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:14









DevelopalDevelopal

83




83












  • your updateBooks call itself

    – Rémi Rousselet
    Nov 13 '18 at 16:20











  • @Developal I'm quite sure that the edit you suggested to my answer won't work because fetchBooks() returns a Future<...>.

    – Günter Zöchbauer
    Nov 13 '18 at 16:53


















  • your updateBooks call itself

    – Rémi Rousselet
    Nov 13 '18 at 16:20











  • @Developal I'm quite sure that the edit you suggested to my answer won't work because fetchBooks() returns a Future<...>.

    – Günter Zöchbauer
    Nov 13 '18 at 16:53

















your updateBooks call itself

– Rémi Rousselet
Nov 13 '18 at 16:20





your updateBooks call itself

– Rémi Rousselet
Nov 13 '18 at 16:20













@Developal I'm quite sure that the edit you suggested to my answer won't work because fetchBooks() returns a Future<...>.

– Günter Zöchbauer
Nov 13 '18 at 16:53






@Developal I'm quite sure that the edit you suggested to my answer won't work because fetchBooks() returns a Future<...>.

– Günter Zöchbauer
Nov 13 '18 at 16:53













1 Answer
1






active

oldest

votes


















0














Here is the cycle:



 void updateBooks() // called from vvv
setState(()
books = updateBooks(); // <<<< calls ^^^
);



you might want something like



 void updateBooks() async 
var result = await fetchBooks();
setState(()
books = result;
);






share|improve this answer























  • I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

    – Developal
    Nov 13 '18 at 17:56











  • That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

    – Günter Zöchbauer
    Nov 13 '18 at 17:58











  • With this approach, will the FutureBuilder widget still be needed?

    – Developal
    Nov 15 '18 at 10:59











  • No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

    – Günter Zöchbauer
    Nov 15 '18 at 12:52










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285136%2fstackoverflowerror-was-thrown-building-futurebuilderlistdynamic%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here is the cycle:



 void updateBooks() // called from vvv
setState(()
books = updateBooks(); // <<<< calls ^^^
);



you might want something like



 void updateBooks() async 
var result = await fetchBooks();
setState(()
books = result;
);






share|improve this answer























  • I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

    – Developal
    Nov 13 '18 at 17:56











  • That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

    – Günter Zöchbauer
    Nov 13 '18 at 17:58











  • With this approach, will the FutureBuilder widget still be needed?

    – Developal
    Nov 15 '18 at 10:59











  • No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

    – Günter Zöchbauer
    Nov 15 '18 at 12:52















0














Here is the cycle:



 void updateBooks() // called from vvv
setState(()
books = updateBooks(); // <<<< calls ^^^
);



you might want something like



 void updateBooks() async 
var result = await fetchBooks();
setState(()
books = result;
);






share|improve this answer























  • I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

    – Developal
    Nov 13 '18 at 17:56











  • That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

    – Günter Zöchbauer
    Nov 13 '18 at 17:58











  • With this approach, will the FutureBuilder widget still be needed?

    – Developal
    Nov 15 '18 at 10:59











  • No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

    – Günter Zöchbauer
    Nov 15 '18 at 12:52













0












0








0







Here is the cycle:



 void updateBooks() // called from vvv
setState(()
books = updateBooks(); // <<<< calls ^^^
);



you might want something like



 void updateBooks() async 
var result = await fetchBooks();
setState(()
books = result;
);






share|improve this answer













Here is the cycle:



 void updateBooks() // called from vvv
setState(()
books = updateBooks(); // <<<< calls ^^^
);



you might want something like



 void updateBooks() async 
var result = await fetchBooks();
setState(()
books = result;
);







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 16:20









Günter ZöchbauerGünter Zöchbauer

319k68951889




319k68951889












  • I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

    – Developal
    Nov 13 '18 at 17:56











  • That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

    – Günter Zöchbauer
    Nov 13 '18 at 17:58











  • With this approach, will the FutureBuilder widget still be needed?

    – Developal
    Nov 15 '18 at 10:59











  • No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

    – Günter Zöchbauer
    Nov 15 '18 at 12:52

















  • I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

    – Developal
    Nov 13 '18 at 17:56











  • That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

    – Günter Zöchbauer
    Nov 13 '18 at 17:58











  • With this approach, will the FutureBuilder widget still be needed?

    – Developal
    Nov 15 '18 at 10:59











  • No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

    – Günter Zöchbauer
    Nov 15 '18 at 12:52
















I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

– Developal
Nov 13 '18 at 17:56





I think the return value of fetchBooks should be passed directly to the books variable rather than awaiting it to be resolved, since books is passed as an argument to the future parameter of FutureBuilder.

– Developal
Nov 13 '18 at 17:56













That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

– Günter Zöchbauer
Nov 13 '18 at 17:58





That's not how it works. You need to await a Future to get the value and in setState() you need to pass the value sync, otherwise Flutter will rebuild before the value is actually set. You should get several warnings/errors in the IDE with the changes you suggested.

– Günter Zöchbauer
Nov 13 '18 at 17:58













With this approach, will the FutureBuilder widget still be needed?

– Developal
Nov 15 '18 at 10:59





With this approach, will the FutureBuilder widget still be needed?

– Developal
Nov 15 '18 at 10:59













No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

– Günter Zöchbauer
Nov 15 '18 at 12:52





No FutureBuilder needed with this approach. I didn't see that you used FutureBuilder, this is why I didn't understand your first comment above. I'd rather not use FutureBuilder when getting the Future is more involved than just a simple call to a getter/method.

– Günter Zöchbauer
Nov 15 '18 at 12:52

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285136%2fstackoverflowerror-was-thrown-building-futurebuilderlistdynamic%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto