gpt4 book ai didi

flutter |如何在共享首选项中存储 int 列表

转载 作者:行者123 更新时间:2023-12-03 02:41:01 27 4
gpt4 key购买 nike

在这里,我想将产品的所有 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");
}
}

最佳答案

您可以在下面复制粘贴运行完整代码
你可以
第 1 步:转换 List<int>List<String>第 2 步:使用 prefs.setStringList 保存
第 3 步:使用 prefs.getStringList 取回它
第 4 步:隐蔽到 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()}");

输出
I/flutter ( 4347): [11, 22, 33, 44, 55]

完整代码
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),
),
);
}
}

关于 flutter |如何在共享首选项中存储 int 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60220216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com