当前位置:网站首页>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 ');
}
}
边栏推荐
- 8、智慧交通项目(1)
- Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
- Creo makes a mobius belt in the simplest way
- [matlab] image binarization (imbinarize function)
- Match a mobile number from a large number of mobile numbers
- Flutter multilingual intl: ^0.17.0 cannot be imported
- Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics
- I put a two-dimensional code with rainbow candy
- Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
- [MySQL learning notes 20] MySQL architecture
猜你喜欢

Vscode attempted to write the procedure to a pipeline that does not exist

Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
![[wechat applet full stack development course] course directory (mpvue+koa2+mysql)](/img/1c/ab39cf0a69d90a85665a099f5dbd26.jpg)
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)

203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?

Use Navicat to compare data differences and structure differences of multi environment databases, and automatic DML and DDL scripts

vscode试图过程写入管道不存在

neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)

TLAB mechanism of JVM object memory allocation and TLAB process in G1

CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded

Compile time annotations for custom annotations (retentionpolicy.class)
随机推荐
Oracle function trigger
Reza RA series - development environment construction
Format analysis and explanation of wav file
Pytorch_ Geometric (pyg) uses dataloader to report an error runtimeerror: sizes of tenants must match except in dimension 0
x86电脑上下载debian的arm64的包
2021mathorcupc topic optimal design of heat dissipation for submarine data center
neo4jDesktop(neo4j桌面版)配置自动启动(开机自启)
Creating a binary tree (binary linked list) from a generalized table
Summarize two methods of configuring pytorch GPU environment
【mysql学习笔记20】mysql体系结构
[matlab] image binarization (imbinarize function)
How to "transform" small and micro businesses (II)?
[buuctf.reverse] 121-125
Test Development Engineer
Notes on writing questions in C language -- monkeys eat peaches
[MySQL learning notes 21] storage engine
Etcd教程 — 第四章 Etcd集群安全配置
[MySQL learning notes 20] MySQL architecture
Mapping mode of cache
[smart agriculture program] smart agriculture small program project is currently popular.