当前位置:网站首页>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 ');
    }
  }
原网站

版权声明
本文为[Moon Spring]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200549006038.html

随机推荐