当前位置:网站首页>Fluent creates, reads and writes JSON files
Fluent creates, reads and writes JSON files
2022-06-25 09:43:00 【Moon Spring】
First obtain the permission to read and write the mobile phone storage
import 'package:permission_handler/permission_handler.dart';
/// Request permission to read and write mobile phone storage
Future checkPermisson() async {
if (Platform.isAndroid) {
// Current permissions
Permission permission = Permission.storage;
// Status of permissions
PermissionStatus status = await permission.status;
if (status.isUndetermined) {
// Never applied
print(" Never applied ");
// Multiple permission applications
Map<Permission, PermissionStatus> statuses = await [Permission.storage,].request();
} else if (status.isDenied) {
// The user who applied for the first time refused
print(" The user who applied for the first time refused ");
} else if (status.isPermanentlyDenied) {
// The user clicked Reject without prompting
print(" The user clicked Reject without prompting ");
} else {
// Permission passing
print(" Permission passing ");
// Get login save file loginSave.json
_getLoginSaveFlie();
}
}
}
write in json file
import 'dart:async';
import 'dart:io';
import 'dart:convert' as convert;
// Create login save file loginSave.json
void _createLoginSaveFlie() async {
// Log in successfully to create a file , Save data as long as the file exists
try {
// Save the path
String savePath = "";
/// Judge the model of mobile phone system
if (Platform.isAndroid) {
savePath = "/sdcard/Download/" + "loginSave.json";
} else if (Platform.isIOS) {
/*//Caches Folder : Save the persistent data generated by the application runtime ,iTunes This directory will not be backed up when the device is synchronized . Generally, the storage volume is large 、 Unimportant data that does not need to be backed up Directory tempDir = await getTemporaryDirectory(); savePath = tempDir.path + "/" + temp.last;*/
// Get the path of the document directory
Directory appDocDir = await getApplicationDocumentsDirectory();
String dir = appDocDir.path;
savePath = dir + "/" + "loginSave.json";
}
File jsonFile = new File(savePath);
// Judge whether the file exists
if (!jsonFile.existsSync()) {
// File does not exist create file
jsonFile.createSync();
print('loginSave.txt Create success ');
} else {
print('loginSave.txt file already exist ');
}
// Write data
var json = {
'carrier_user_agreement': true,
};
// json File is written to
jsonFile.writeAsString(convert.jsonEncode(json));
} catch (e) {
print('loginSave.txt Create failure ');
}
}
Read json file
import 'dart:async';
import 'dart:io';
import 'dart:convert' as convert;
// Get login save file loginSave.json
void _getLoginSaveFlie() async {
try {
// Save the path
String savePath = "";
/// Judge the model of mobile phone system
if (Platform.isAndroid) {
savePath = "/sdcard/Download/" + "loginSave.json";
} else if (Platform.isIOS) {
/*//Caches Folder : Save the persistent data generated by the application runtime ,iTunes This directory will not be backed up when the device is synchronized . Generally, the storage volume is large 、 Unimportant data that does not need to be backed up Directory tempDir = await getTemporaryDirectory(); savePath = tempDir.path + "/" + temp.last;*/
// Get the path of the document directory
Directory appDocDir = await getApplicationDocumentsDirectory();
String dir = appDocDir.path;
savePath = dir + "/" + "loginSave.json";
}
File jsonFile = new File(savePath);
// Judge whether the file exists
if (jsonFile.existsSync()) {
// json File read
var jsonStr = await jsonFile.readAsString();
var json = convert.jsonDecode(jsonStr);
print(json['carrier_user_agreement']); // xiaoming
setState(() {
_checkbox = json['carrier_user_agreement'];
});
} else {
print('loginSave.txt file does not exist ');
setState(() {
_checkbox = false;
});
}
} catch (e) {
print(' Read loginSave.txt File failed ');
}
}
边栏推荐
- 富时A50开户什么地方安全
- [competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
- Download the arm64 package of Debian on X86 computer
- [buuctf.reverse] 121-125
- Register the jar package as a service to realize automatic startup after startup
- PHP obtains the IP address, and the apache2 server runs without error
- Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
- Flutter multilingual intl: ^0.17.0 cannot be imported
- Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?
- How to download the school logo, school name and corporate logo on a transparent background without matting
猜你喜欢

Compile time annotations for custom annotations (retentionpolicy.class)

2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course

Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
![[matlab] image binarization (imbinarize function)](/img/3e/066f460d9f436bbc43ea35e46093e2.jpg)
[matlab] image binarization (imbinarize function)

Benefits and types of cloud network technology

Wearable devices may reveal personal privacy
![[MySQL learning notes 21] storage engine](/img/3a/a3cd573281efc689cafdb7d7562ce0.png)
[MySQL learning notes 21] storage engine

【mysql学习笔记22】索引

匯付國際為跨境電商賦能:做合規的跨境支付平臺!

Compare and explain common i/o models
随机推荐
8. Intelligent transportation project (1)
C语言刷题随记 —— 猴子吃桃
[zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
Voiceprint Technology (III): voiceprint recognition technology
Format analysis and explanation of wav file
独步武林,架构选型手册(包含 PDF)
Grabcut image segmentation in opencv
[MySQL learning notes 21] storage engine
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
手机办理长城证券开户靠谱安全吗?
What should be paid attention to in PMP examination?
Creating a binary tree (binary linked list) from a generalized table
[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function
Notes on writing questions in C language -- monkeys eat peaches
[smart agriculture program] smart agriculture small program project is currently popular.
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
在指南针上面开户好不好,安不安全?
汇付国际为跨境电商赋能:做合规的跨境支付平台!
Voiceprint Technology (IV): Engineering deployment of voiceprint recognition
[shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?