我想保存一个字符串列表,并将它们显示在ListView.builder
小部件中。问题是,当我从列表中获得值时,我会在值周围加上括号,但我只需要字符串本身
现在我得到:[value]
但我需要得到:value
这是我的代码:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class PrefListViewBuilder extends StatefulWidget {
const PrefListViewBuilder({Key? key}) : super(key: key);
@override
State<PrefListViewBuilder> createState() => _PrefListViewBuilderState();
}
class _PrefListViewBuilderState extends State<PrefListViewBuilder> {
List stringList = [];
setStringList() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList("key", <String>["value"]);
setState(() {
stringList.add(prefs.getStringList("key"));
});
print(stringList);
}
getStringList() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.getStringList("kfzs");
print(stringList);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
onPressed: setStringList,
child: const Text("Set"),
),
TextButton(
onPressed: getStringList,
child: const Text("Get"),
),
],
),
const SizedBox(height: 20),
Text(stringList.toString()),
const SizedBox(height: 20),
Container(
color: Colors.white,
child: ListView.builder(
shrinkWrap: true,
itemCount: stringList.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(10),
child: Container(
color: Colors.blue,
child: Text(stringList[index].toString()),
),
);
},
),
),
],
),
);
}
}
有人能帮我吗?
提前感谢!
getList将是
stringList = prefs.getStringList("key") ?? [];
而不是
stringList.add(prefs.getStringList("key"));
完整的片段
class PrefListViewBuilder extends StatefulWidget {
const PrefListViewBuilder({Key? key}) : super(key: key);
@override
State<PrefListViewBuilder> createState() => _PrefListViewBuilderState();
}
class _PrefListViewBuilderState extends State<PrefListViewBuilder> {
List stringList = [];
setStringList() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList("key", <String>["value"]);
await getStringList();
setState(() {
// stringList = prefs.getStringList("key") ?? [];
});
print(stringList);
}
getStringList() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
stringList = prefs.getStringList("key") ?? [];
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton(
onPressed: setStringList,
child: const Text("Set"),
),
TextButton(
onPressed: getStringList,
child: const Text("Get"),
),
],
),
const SizedBox(height: 20),
Text(stringList.toString()),
const SizedBox(height: 20),
Container(
color: Colors.white,
child: ListView.builder(
shrinkWrap: true,
itemCount: stringList.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(10),
child: Container(
color: Colors.blue,
child: Text(stringList[index]),
),
);
},
),
),
],
),
);
}
}