{"id":615,"date":"2023-02-18T19:49:07","date_gmt":"2023-02-18T11:49:07","guid":{"rendered":"http:\/\/gjweb.top\/?p=615"},"modified":"2023-02-18T19:49:07","modified_gmt":"2023-02-18T11:49:07","slug":"28-flutter-%e6%9c%ac%e5%9c%b0%e5%ad%98%e5%82%a8-shared_preferences","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=615","title":{"rendered":"28. flutter \u672c\u5730\u5b58\u50a8 shared_preferences"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import 'package:shared_preferences\/shared_preferences.dart';<br>\/\/\u5b58<br>\/\/ Obtain shared preferences.<br>final prefs = await SharedPreferences.getInstance();<br>\u200b<br>\/\/ Save an integer value to 'counter' key.<br>await prefs.setInt('counter', 10);<br>\/\/ Save an boolean value to 'repeat' key.<br>await prefs.setBool('repeat', true);<br>\/\/ Save an double value to 'decimal' key.<br>await prefs.setDouble('decimal', 1.5);<br>\/\/ Save an String value to 'action' key.<br>await prefs.setString('action', 'Start');<br>\/\/ Save an list of strings to 'items' key.<br>await prefs.setStringList('items', &lt;String&gt;&#91;'Earth', 'Moon', 'Sun']);<br>\u200b<br>\/\/ \u53d6<br>final int? counter = prefs.getInt('counter');<br>\/\/ Try reading data from the 'repeat' key. If it doesn't exist, returns null.<br>final bool? repeat = prefs.getBool('repeat');<br>\/\/ Try reading data from the 'decimal' key. If it doesn't exist, returns null.<br>final double? decimal = prefs.getDouble('decimal');<br>\/\/ Try reading data from the 'action' key. If it doesn't exist, returns null.<br>final String? action = prefs.getString('action');<br>\/\/ Try reading data from the 'items' key. If it doesn't exist, returns null.<br>final List&lt;String&gt;? items = prefs.getStringList('items');<br>\u200b<br>\/\/ \u5220\u9664<br>final success = await prefs.remove('counter');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u590d\u6742\u7c7b\u578b\u7684\u5b58\u50a8\">\u590d\u6742\u7c7b\u578b\u7684\u5b58\u50a8<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>final prefs = await SharedPreferences.getInstance();<br>var infoData = &#91;<br> &nbsp;  {\"name\":\"zhangsan\"},<br> &nbsp; &nbsp;12,<br> &nbsp; &nbsp;true<br>]<br>var str = json.encode(infoData);<br>await prefs.setString('data', str);<br>String? xxx = await prefs.getString('data', str);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u590d\u6742\u7c7b\u578b\u7684\u5b58\u50a8<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-flutter"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=615"}],"version-history":[{"count":1,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":616,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/615\/revisions\/616"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}