【问题标题】:Flutter: how to delete a file inside the Flutter app directoryFlutter:如何删除 Flutter 应用目录中的文件
【发布时间】:2020-05-25 00:49:09
【问题描述】:

我想在我的 Flutter 应用程序目录中删除一个文本文件(其中包含一些关于用户的数据)

怎么做?

【问题讨论】:

    标签: flutter directory filesystems


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    你可以使用file.delete()
    代码sn-p

      Future<String> get _localPath async {
        final directory = await getApplicationDocumentsDirectory();
    
        return directory.path;
      }
    
      Future<File> get _localFile async {
        final path = await _localPath;
        print('path ${path}');
        return File('$path/counter.txt');
      }
    
      Future<int> deleteFile() async {
            try {
              final file = await _localFile;
    
              await file.delete();
            } catch (e) {
              return 0;
            }
          }
    

    工作演示

    删除文件前的工作演示

    删除文件后的工作演示

    完整代码

    import 'dart:async';
    import 'dart:io';
    
    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    import 'package:path_provider/path_provider.dart';
    
    void main() {
      runApp(
        MaterialApp(
          title: 'Reading and Writing Files',
          home: FlutterDemo(storage: CounterStorage()),
        ),
      );
    }
    
    class CounterStorage {
      Future<String> get _localPath async {
        final directory = await getApplicationDocumentsDirectory();
    
        return directory.path;
      }
    
      Future<File> get _localFile async {
        final path = await _localPath;
        print('path ${path}');
        return File('$path/counter.txt');
      }
    
      Future<int> deleteFile() async {
        try {
          final file = await _localFile;
    
          await file.delete();
        } catch (e) {
          return 0;
        }
      }
    
      Future<int> readCounter() async {
        try {
          final file = await _localFile;
    
          // Read the file
          String contents = await file.readAsString();
    
          return int.parse(contents);
        } catch (e) {
          // If encountering an error, return 0
          return 0;
        }
      }
    
      Future<File> writeCounter(int counter) async {
        final file = await _localFile;
    
        // Write the file
        return file.writeAsString('$counter');
      }
    }
    
    class FlutterDemo extends StatefulWidget {
      final CounterStorage storage;
    
      FlutterDemo({Key key, @required this.storage}) : super(key: key);
    
      @override
      _FlutterDemoState createState() => _FlutterDemoState();
    }
    
    class _FlutterDemoState extends State<FlutterDemo> {
      int _counter;
    
      @override
      void initState() {
        super.initState();
        widget.storage.readCounter().then((int value) {
          setState(() {
            _counter = value;
          });
        });
      }
    
      Future<File> _incrementCounter() {
        setState(() {
          _counter++;
        });
    
        // Write the variable as a string to the file.
        return widget.storage.writeCounter(_counter);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Reading and Writing Files')),
          body: Column(
            children: <Widget>[
              Center(
                child: Text(
                  'Button tapped $_counter time${_counter == 1 ? '' : 's'}.',
                ),
              ),
              RaisedButton(
                onPressed: () {
                  widget.storage.deleteFile();
                },
                child: Text("delete file"),
                textColor: Color(0xffff0000),
                color: Color(0xff11f1f1),
                highlightColor: Color(0xff00ff00),
              ),
            ],
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-03
      • 2019-04-04
      • 2021-07-02
      • 2023-01-04
      • 2021-01-19
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多