当前位置:网站首页>flutter 文件操作
flutter 文件操作
2022-06-09 11:26:00 【海阔天空6688】
添加文件操作库
打开pubspec.yaml文件,在dependencies下添加path_provider依赖,点击Pub get(或通过命令 flutter pub add path_provider )获取依赖
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
english_words: ^4.0.0
path_provider: ^2.0.11 //注意空格缩进
如果依赖中出现gradle等下载不下来,找到External Libraries下的Flutter Plugins 目录下的path_provider_android-2.0.14,找到android下的build.gralde,添加阿里云镜像、修改gradle版本和本地运行项目一直,如下:
buildscript {
repositories {
// google()
mavenCentral()
maven{ url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://jitpack.io' }
maven { url "https://s01.oss.sonatype.org/content/groups/public" }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url "https://maven.aliyun.com/repository/public" }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.3' //修改版本
}
}
rootProject.allprojects {
repositories {
// google()
mavenCentral()
maven{ url 'https://maven.aliyun.com/repository/google'}
maven { url 'https://jitpack.io' }
maven { url "https://s01.oss.sonatype.org/content/groups/public" }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url "https://maven.aliyun.com/repository/public" }
}
}
import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
class FileWidget extends StatefulWidget{
const FileWidget({Key? key}) : super(key: key);
@override
State<FileWidget> createState() {
return FileState();
}
}
class FileState extends State<FileWidget>{
int _counter=0;
@override
void initState() {
super.initState();
//从文件读取点击次数
_readCount().then((int value) {
setState(() {
_counter = value;
});
});
}
Future<int> _readCount() async{
try {
File file = await _getLocalFile();
// 读取点击次数(以字符串)
String contents = await file.readAsString();
return int.parse(contents);
} on FileSystemException {
return 0;
}
}
Future<File> _getLocalFile() async{
// 获取应用目录
String dir = (await getApplicationDocumentsDirectory()).path;
return File('$dir/counter.txt');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("文件操作")),
body: Center(
child: Text("点击了 $_counter 次"),
),
floatingActionButton: FloatingActionButton(
tooltip: "Increment",
onPressed: _incrementCounter,
child: const Icon(Icons.add),
),
);
}
_incrementCounter() async{
setState(() {
_counter++;
});
// 将点击次数以字符串类型写到文件中
await (await _getLocalFile()).writeAsString('$_counter');
}
}
创建的counter.txt目录在:data-项目包名-app_flutter目录下
边栏推荐
- 01 | 来龙去脉:中台为什么这么火?
- Prompt credssp encryption database correction when windows is remote
- Redis data structure and introduction
- 13. Using WPF to develop USB detection tool
- tag贪心-刷题预备知识-贪心解题方法 + lt.455. 分发饼干 + lt.376. 摆动序列
- Gson, fastjason, Jackson serialization differences
- Error in library(patRoon) : 不存在叫‘patRoon‘这个名字的程辑包
- Test instructions for system integration project management engineer
- MySQL SQL statement optimization
- 8. < tag backtracking and full arrangement > lt.46 Full Permutation + lt.47 Full arrangement II
猜你喜欢

【数据中台】00丨开篇词丨数据中台,是陷阱?还是金钥匙?

What are the deadlock troubleshooting tools?

3. < tag backtracking, combination and pruning > lt.17 Letter combination of telephone number

1. < tag backtracking, combination and pruning > lt.77 Combination + pruning dyh

04 | 万事预则立:中台建设前必须想清楚的四个问题

How to model 3DMAX (I)

8. search insertion position

11.<tag-二叉树和BST基础>lt.501. 二叉搜索树中的众数

Range方法返回对象转换办法

4. < tag backtracking, combination and pruning > lt.39 Combined sum + lt.40 Combined sum II DBC
随机推荐
Answers to six basic questions of security assessment for commercial password applications
[reprint] understand G1 garbage collector
Composition maximum area
How much bandwidth is required to view 1080p, 2k and 4K videos smoothly?
dotnet core 也能协调分布式事务啦!
Dotnet core can also coordinate distributed transactions!
OpenSSL security vulnerability (cve-2016-8610) repair steps
6.<tag-回溯和切割问题>lt.131.分割回文串
H3C Certified Safety Technology Senior Engineer
使用U盘一比一拷贝核心板系统镜像的方法
H3C Certified Network Engineer
H3C certified cloud computing Engineer
Google Chrome plugin | pagenote page tag
H3C certified cloud computing senior engineer
06 | the first step of China Taiwan landing: enterprise strategy decomposition and current situation research (Discovery)
Aiko ai Frontier promotion (6.9)
还有新产品?丰田GR系列或将扩充产品线
H3C Certified Wireless Internet expert
Redis data structure and introduction
虚拟机出现entering emergency mode,使用xfs_rapair出现Device or resource busy解决