如何从搜索栏中保存搜索的项目,并在颤振中显示共享首选项中保存的列表



我试图将搜索的项目从搜索栏保存到共享首选项,并希望在其他页面中显示搜索列表的列表,但无法实现。下面是我的代码,我如何从共享首选项中保存和检索它。

我已经更新了我的代码,请仔细阅读。

更新我有查询,我正在将其传递给 url 并直接获取列表

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Ayurwikilist> ayurwikilist = [];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('Ayurwiki'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: CustomSearchDelegate(ayurwikilist));
},
),
],
),
body: _body(),
);
}
class CustomSearchDelegate extends SearchDelegate {
List<Ayurwikilist> ayurwikilist = [];
CustomSearchDelegate(this.ayurwikilist);
Future<Ayurwikilist> fetchPost() async {
final response = await http.get(
'https://www.example.org/api.php?action=query&list=search&srsearch=$query&utf8=&format=json');
print(
'https://www.example.org/api.php?action=query&list=search&srsearch=$query&utf8=&format=json');
return Ayurwikilist.fromJson(json.decode(response.body));
}
@override
ThemeData appBarTheme(BuildContext context) {
assert(context != null);
final ThemeData theme = Theme.of(context);
assert(theme != null);
return theme;
}
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () async{
query = '';
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('name', "$query");
print(query);
},
),
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return Container();
}
@override
Widget buildSuggestions(BuildContext context) {
return FutureBuilder<Ayurwikilist>(
future: fetchPost(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
print(snapshot.data.toString());
return ListView.builder(
itemCount: snapshot.data.query.search.length,
itemBuilder: (BuildContext context, int index) {
var title = snapshot.data.query.search[index].title;
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Detailpage(
snapshot.data.query.search[index].title,
// 'images/ayurwiki.png'
),
));
},
child: ListTile(
title: Text(title),
),
);
});
} else {
return Center(
child: Text(
'Search in ayurwiki',
style: TextStyle(color: Colors.grey, fontSize: 18),
),
);
}
},
);
}
}
class _HistoryState extends State<History> {
var myName;
getCredential() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var query = prefs.getString('query');
setState(() {
myName = query;
});
print('item : $query');
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title:Text('Rcently viewed item'),
),
body: Container(
decoration: new BoxDecoration(color: Colors.white),
child: myName == null ? Text('No items') : Text('$myName'),
),
);
}
}

更新

Future<Ayurwikilist> fetchPost() async {
final response = await http.get(
'https://www.example.org/api.php?action=query&list=search&srsearch=$query&utf8=&format=json');
print(
'https://www.example.org/api.php?action=query&list=search&srsearch=$query&utf8=&format=json');
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList('name', query as List);
return Ayurwikilist.fromJson(json.decode(response.body));
}
getCredential() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var name = prefs.getStringList('name');
setState(() {
myName = name;
});
print('item : $name');
}

你应该在initState中调用getCredential((函数

class _HistoryState extends State<History> {
var myName;
initState(){
super.initState();
getCredential();
}
getCredential() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var query = prefs.getString('query');
setState(() {
myName = query;
});
print('item : $query');
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title:Text('Rcently viewed item'),
),
body: Container(
decoration: new BoxDecoration(color: Colors.white),
child: myName == null ? Text('No items') : Text('$myName'),
),
);
}
}

更新:

Future<Ayurwikilist> fetchPost() async {
query = 'something you need';
final response = await http.get(
'https://www.example.org/api.php?action=query&list=search&srsearch=$query&utf8=&format=json');
print(
'https://www.example.org/api.php?action=query&list=search&srsearch=$query&utf8=&format=json');
prefs.setString('name', query);
return Ayurwikilist.fromJson(json.decode(response.body));
}

最新更新