Rut*_*ana 5 sharedpreferences dart flutter flutter-dependencies flutter-layout
在这里,我想将产品的所有 Id 存储在共享首选项中。但是在这里,当我使用setInt它存储 id 时,它只会存储一个 Id 并删除以前的 ID,因此我希望所有 id 都需要存储在共享首选项中,以及如何获取存储在共享首选项中的所有 id。
这是我试过的代码
unAuthAddCart(dynamic itemId) async {
try {
SharedPreferences pref = await SharedPreferences.getInstance();
var unAuthItemId = pref.setInt('unAuthItemid', itemId);
print(unAuthItemId);
var abc = pref.getInt('unAuthItemid');
print("UnAuth Itme Id $abc");
} catch (e) {
print(e);
print("Something went Wrong in Particular Product Cart");
}
}
Run Code Online (Sandbox Code Playgroud)
您可以复制粘贴运行下面的完整代码
您可以
步骤1:转换List<int>为步骤2:使用
步骤3List<String>
保存:使用
步骤4取回:转换为prefs.setStringList prefs.getStringList List<int>
代码片段
List<int> intProductListOriginal = [11, 22, 33, 44, 55];
void _incrementCounter() async {
List<String> strList = intProductListOriginal.map((i) => i.toString()).toList();
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList("productList", strList);
List<String> savedStrList = prefs.getStringList('productList');
List<int> intProductList = savedStrList.map((i) => int.parse(i)).toList();
print("${intProductList.toString()}");
Run Code Online (Sandbox Code Playgroud)
输出
I/flutter ( 4347): [11, 22, 33, 44, 55]
Run Code Online (Sandbox Code Playgroud)
完整代码
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
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 _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<int> intProductListOriginal = [11, 22, 33, 44, 55];
void _incrementCounter() async {
List<String> strList = intProductListOriginal.map((i) => i.toString()).toList();
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList("productList", strList);
List<String> savedStrList = prefs.getStringList('productList');
List<int> intProductList = savedStrList.map((i) => int.parse(i)).toList();
print("${intProductList.toString()}");
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
目前,没有任何选项可以直接将整数列表保存在 Sharedpreferences 中。您可以将字符串列表保存在共享首选项中。
对于您的情况,最简单的方法是将List<int> 值转换为 List<String>并存储它们。
此外,在检索时,您可以获取存储在共享首选项中的List<String>并将其转换为原始 List<int>
对于前:
将您的列表存储在共享首选项中。
SharedPreferences prefs=await SharedPreferences.getInstance();
// your custom int list
List<int> mList=[0,1,2,3,4];
// convert your custom list to string list
List<String> stringsList= mList.map((i)=>i.toString()).toList();
// store your string list in shared prefs
prefs.setStringList("stringList", stringsList);
Run Code Online (Sandbox Code Playgroud)
检索存储的字符串列表并将其转换为原始 int 列表
SharedPreferences prefs=await SharedPreferences.getInstance();
// fetch your string list
List<String> mList = (prefs.getStringList('stringList') ?? List<String>());
//convert your string list to your original int list
List<int> mOriginaList = mList.map((i)=> int.parse(i)).toList();
print(mOriginaList);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3645 次 |
| 最近记录: |