我在Flutter应用程序中使用了shared_preferences
插件,当我单击按钮时设置了数据,但是我试图确保数据没有到来。
我在这里设置数据,当我打印数据时,我可以在控制台上看到输出。
shows.forEach((element) {
showCode=element.showName;
show.setString('showName', showCode);
});
我想在重写方法上方获取我的数据。但是当我在这里打印时,我只看到1个项目,但是我需要打印2个项目。
String showCode;
Future<String> getCode() async{
final codeValue = await SharedPreferences.getInstance();
setState((){
showyCode =codeValue.getString('showName');
});
print('here $showCode');
return showCode;
}
但是,即使我有2个数据要来,但我只看到1个项目,但我无法在函数中称此未来。我试图将其包括在Listview中,然后它用1个项目填充了整个列表。有人知道我做错了吗?
_showProgramData
? Container(
height: MediaQuery.of(context).size.height/2,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
children: [
IconButton(icon:Icon(Icons.close) , onPressed:() {Navigator.pushNamed(context, SecondScreen.routeName);}),
Text('Please Select Show',style: TextStyle(fontWeight:FontWeight.bold,fontSize: 24),),
],
),
Text(showCode),
],
),
),
)
答案 0 :(得分:1)
您可以在下面复制粘贴运行完整代码
步骤1:您需要使用setStringList
和getStringList
步骤2:getCode()
返回Future<List<String>>
步骤3:在forEach
中使用showCodeList.add(element.showName);
代码段
void _setData() async {
shows.forEach((element) {
showCodeList.add(element.showName);
});
final codeValue = await SharedPreferences.getInstance();
await codeValue.setStringList('showName', showCodeList);
setState(() {});
}
Future<List<String>> getCode() async {
final codeValue = await SharedPreferences.getInstance();
showCodeList = await codeValue.getStringList('showName');
if (showCodeList == null) {
showCodeList = [];
}
print('here ${showCodeList.toString()}');
return showCodeList;
}
...
FutureBuilder(
future: _future,
builder: (context, AsyncSnapshot<List<String>> snapshot) {
...
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Card(
elevation: 6.0,
child: Padding(
padding: const EdgeInsets.only(
top: 6.0, bottom: 6.0, left: 8.0, right: 8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(snapshot.data[index]),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() async{
WidgetsFlutterBinding.ensureInitialized();
final codeValue = await SharedPreferences.getInstance();
await codeValue.setStringList('showName', []);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class Show {
String showName;
Show({this.showName});
}
class _MyHomePageState extends State<MyHomePage> {
Future<List<String>> _future;
List<Show> shows = [Show(showName: "a"), Show(showName: "b")];
String showCode;
List<String> showCodeList = [];
void _setData() async {
shows.forEach((element) {
showCodeList.add(element.showName);
});
final codeValue = await SharedPreferences.getInstance();
await codeValue.setStringList('showName', showCodeList);
setState(() {});
}
Future<List<String>> getCode() async {
final codeValue = await SharedPreferences.getInstance();
showCodeList = await codeValue.getStringList('showName');
if (showCodeList == null) {
showCodeList = [];
}
print('here ${showCodeList.toString()}');
return showCodeList;
}
@override
void initState() {
_future = getCode();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder(
future: _future,
builder: (context, AsyncSnapshot<List<String>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return Text('none');
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
case ConnectionState.active:
return Text('');
case ConnectionState.done:
if (snapshot.hasError) {
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.red),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Card(
elevation: 6.0,
child: Padding(
padding: const EdgeInsets.only(
top: 6.0, bottom: 6.0, left: 8.0, right: 8.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(snapshot.data[index]),
],
),
));
});
}
}
}),
floatingActionButton: FloatingActionButton(
onPressed: _setData,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}