欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

flutter 如何实现文件读写(使用篇)

程序员文章站 2023-04-05 14:51:32
flutter文件读写可以对磁盘文件进行操作,实现某些业务场景,那么我们开始来讲下这个文件读写操作。 使用的库插件(package) dart:io(用于数据处理) path_provider (用于获取路劲) 操作步骤 1.获取正确的本地路径 2.创建指向文件位置的引用 3.写入数据到文件内 4. ......

flutter文件读写可以对磁盘文件进行操作,实现某些业务场景,那么我们开始来讲下这个文件读写操作。


使用的库插件(package)
(用于数据处理)
 (用于获取路劲)

操作步骤
1.获取正确的本地路径
2.创建指向文件位置的引用
3.写入数据到文件内
4.从文件读取数据

1.获取正确的本地路径 
我们获取路劲用的是这个插件
 
可以看到里面提供了两个获取路劲的方式

example

directory tempdir = await gettemporarydirectory();
string temppath = tempdir.path;

directory appdocdir = await getapplicationdocumentsdirectory();
string appdocpath = appdocdir.path;

gettemporarydirectory:【临时文件夹
也就是系统可以随时清空的临时缓存文件夹,在ios中对应nstemporarydirectory在安卓中对应getcachedir() 

我们来将信息储存在临时文件夹中,首先我们创建一个storage类里面开始写

class storage {
  future<string> get _localpath async {
    final _path = await gettemporarydirectory();
    return _path.path;
  }
}


2.创建指向文件位置的引用 
确定文件储存位置之后,导入我们的io库,使用包里面的file类做泛型,然后获取路劲并且指向我们的文件名

future<file> get _localfile async {
  final path = await _localpath;
  return file('$path/counter.txt');
}


3.写入数据到文件内 
现在有了可以使用的file,直接就可以来读写数据了,因为我们使用了计数器,所以只需将证书储存为字符串格式,
使用“$counter”即可(解析成整数方法在下一步)

future<file> writecounter(counter) async {
    final file = await _localfile;

    return file.writeasstring('$counter');
  }


4.从文件读取数据
现在可以直接用file类来读取文件数据,然后用int的自带解析方法来解析我们读取的string

future<int> readcounter() async {
    try {
      final file = await _localfile;

      var contents = await file.readasstring();

      return int.parse(contents);
    } catch (e) {
      return 0;
    }
  }


完整代码

import 'dart:io';
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

class storage {
  future<string> get _localpath async {
    final _path = await gettemporarydirectory();
    return _path.path;
  }

  future<file> get _localfile async {
    final path = await _localpath;

    return file('$path/counter.txt');
  }

  future<int> readcounter() async {
    try {
      final file = await _localfile;

      var contents = await file.readasstring();

      return int.parse(contents);
    } catch (e) {
      return 0;
    }
  }

  future<file> writecounter(counter) async {
    final file = await _localfile;

    return file.writeasstring('$counter');
  }
}

class onepage extends statefulwidget {
  final storage storage;

  onepage({this.storage});

  @override
  _onepagestate createstate() => _onepagestate();
}

class _onepagestate extends state<onepage> {
  int _counter;

  @override
  void initstate() {
    super.initstate();
    widget.storage.readcounter().then((value) {
      setstate(() => _counter = value);
    });
  }

  future<file> _incrementcounter() async {
    setstate(() => _counter++);
    return widget.storage.writecounter(_counter);
  }

  future<file> _incrementcounterj() async {
    setstate(() => _counter--);
    return widget.storage.writecounter(_counter);
  }

  @override
  widget build(buildcontext context) {
    return scaffold(
      body: center(
        child: text(
          '$_counter',
          style: theme.of(context).texttheme.display1,
        ),
      ),
      floatingactionbutton: row(
        children: <widget>[
          floatingactionbutton(
            onpressed: () => _incrementcounter(),
            child: new icon(icons.add),
          ),
          floatingactionbutton(
            onpressed: () => _incrementcounterj(),
            child: new icon(icons.title),
          )
        ],
      ),
      floatingactionbuttonlocation: floatingactionbuttonlocation.centerdocked,
    );
  }
}


原文来自  ===>  flutter教程网: