Мне нужно получить данные из API и поместить их в ListView. Это происходит при нажатии FloatingActionButton, вызывающем метод _refre sh (). Это получает 10 кошек и помещает это в список. В этом методе я вызываю другой метод "_builtListView ()", который должен создать элемент ListView и его строки Cat, но кажется, что кошки исчезают в другом методе, несмотря на то, что это происходит в одном классе.
class _MyHomePageState extends State<MyHomePage> {
var catList = new List<Cat>();
void _refresh() {
var catsFuture = fetchCats();
catsFuture.then((catList) {
print("Future cats catch: " + catList.length.toString() + " cats.");
_builtListView();
});
setState(() { });
}
Future<List<Cat>> fetchCats() async {
// getting cats code
print("fetchCats(): " + catListFetched.length.toString() + " cats.");
return catListFetched;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _builtListView(),
floatingActionButton: FloatingActionButton(
onPressed: _refresh,
tooltip: 'Refresh',
child: Icon(Icons.refresh),
),
);
}
Widget _builtListView() {
if (catList.isEmpty)
print("_builtListView, catList is empty!");
return ListView.builder(
itemCount: catList == null ? 0 : catList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text("Text " + index.toString()), //catList[index].toString()),
);
},
);
}
}
Что я вижу в консоли:
I/flutter (10172): _builtListView, catList is empty!
//_refresh
I/flutter (10172): _builtListView, catList is empty!
I/flutter (10172): fetchCats(): 10 cats.
I/flutter (10172): Future cats catch: 10 cats.
I/flutter (10172): _builtListView, catList is empty!
Что я не получу?