【发布时间】:2019-07-24 15:48:22
【问题描述】:
我觉得这个问题不应该花 6 个多小时来解决,但我在这里经过大量搜索和玩弄代码。 基本上,当我导航到页面时,future 会自动从 _getData() 发送 http.get 请求。我正在获取数据并构建一个 ListView 就好了,问题是我希望 ListView 仅在从 TextField 提交搜索词之后构建。任何帮助将不胜感激!
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter/material.dart';
class FutureBuilderTestPage extends StatefulWidget {
@override
BuilderTestPageState createState() => BuilderTestPageState();
}
class BuilderTestPageState extends State {
static String _searchTerm = "";
String _url = "https://api.nal.usda.gov/ndb/search/?format=json&q=$_searchTerm&sort=n&max=20&offset=0&api_key=OMITTEDforThisPost";
@override
Widget build(BuildContext context) {
var _futureBuilder = new FutureBuilder(
future: _getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('Press button to start.');
case ConnectionState.active:
case ConnectionState.waiting:
return Text('Awaiting result...');
case ConnectionState.done:
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
// return Text('Result: ${snapshot.data}');
return createMyListView(context, snapshot);
}
return null; // unreachable
});
return new Scaffold(
appBar: new AppBar(
title: Text("Appbar"),
),
body: Column(
children: <Widget>[
Text("Search for food..."),
TextField(
decoration: InputDecoration(hintText: 'Search here'),
onSubmitted: (String valueIn) {
_searchTerm = valueIn;
},
),
Text("Results for... " + _searchTerm),
Divider(height: 4.0),
Expanded(child: _futureBuilder),
],
));
}
Future<List<dynamic>> _getData() async {
http.Response response = await http.get(_url,
headers: {"Accept": "Application/json"});
final data = jsonDecode(response.body) as Map;
final mapOfList = data['list'] as Map;
final listOfItem = mapOfList['item'] as List<dynamic>;
print(listOfItem.toString());
var values = new List<dynamic>();
for (var e in listOfItem) {
values.add(e);
}
return values;
}
Widget createMyListView(BuildContext context, AsyncSnapshot snapshot) {
List<dynamic> values = snapshot.data;
return new ListView.builder(
itemCount: values.length,
itemBuilder: (BuildContext context, int index) {
return new Column(
children: <Widget>[
new ListTile(
title: new Text(values[index]['name']),
)
],
);
},
);
}
}
class FoodObjectFromJson {
final int offset;
final String group;
final String name;
final int ndbno;
final String ds;
final String manu;
FoodObjectFromJson(
this.offset, this.group, this.name, this.ndbno, this.ds, this.manu);
}
【问题讨论】: