是否可以从List<Map<String, dynamic>>
向Future<List<Map<String, dynamic>>>
添加值。
我正在尝试将更多值添加到列表中作为滚动结束。
Future<List<Map<String, dynamic>>> _future;
List<map<String, dynamic>> resultUrl = [];
@override
void initState() {
_future = fetch()
_scrollController.addListener(() {
if (_scrollController.position.pixels ==
_scrollController.position.maxScrollExtent) {
getMoreData();
}
});
super.initState();
}
Future<List<Map<String, dynamic>>> fetch() async {
http.Response response = await http.get(url);
if (response.statusCode != 200) return null;
return List<Map<String,
dynamic>>.from(json.decode(response.body)['map']);
}
//In a stateful Widget
FutureBuilder<List<Map<String, dynamic>>>(
future: _future,
builder: (context, snapshot) {
if (snapshot.hasError) {
print(snapshot.error);
} else {
resultList = snapshot.data;
}
return resultList != null?
StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) => new
Container(
padding: EdgeInsets.all(2.0),
child: Card(
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
elevation: 5,
child: Image(
image: NetworkImage(resultList[index]['image']),
fit: BoxFit.cover,
),
),
),
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 2 : 1),
mainAxisSpacing: 1.0,
crossAxisSpacing: 1.0,
);
getMoreData() {
resultUrl.add(_future);// resultUrl.addAll(_future);
}