Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 方法';读取计数器';在Flatter中从本地存储获取文件时使用try-catch,即使在使用该函数后,也会调用null_File_Flutter_Dart_Local Storage - Fatal编程技术网

File 方法';读取计数器';在Flatter中从本地存储获取文件时使用try-catch,即使在使用该函数后,也会调用null

File 方法';读取计数器';在Flatter中从本地存储获取文件时使用try-catch,即使在使用该函数后,也会调用null,file,flutter,dart,local-storage,File,Flutter,Dart,Local Storage,我想将数据存储在本地文件中以便持久化。因此,我首先尝试了flifter.dev上给出的示例,它在一个单独的项目中工作,但是当我在我正在开发的应用程序中实现相同的东西时,它抛出了一个错误,我无法理解它为什么这样做。最初文件是空的,所以try-catch语句应该可以工作,我在示例项目中测试了它,它在那里工作,但在这个项目中它不工作 用于本地存储的类 import 'dart:io'; import 'package:path_provider/path_provider.dart'; class

我想将数据存储在本地文件中以便持久化。因此,我首先尝试了flifter.dev上给出的示例,它在一个单独的项目中工作,但是当我在我正在开发的应用程序中实现相同的东西时,它抛出了一个错误,我无法理解它为什么这样做。最初文件是空的,所以try-catch语句应该可以工作,我在示例项目中测试了它,它在那里工作,但在这个项目中它不工作

用于本地存储的类

import 'dart:io';
import 'package:path_provider/path_provider.dart';

class CounterStorage {
  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/counter.txt');
  }

  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');
  }
}
错误:

I/flutter ( 6380): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 6380): The following NoSuchMethodError was thrown building _BodyBuilder:
I/flutter ( 6380): The method 'readCounter' was called on null.
I/flutter ( 6380): Receiver: null
I/flutter ( 6380): Tried calling: readCounter()


您正在对空对象调用
readCounter

CounterStorage storage;
    int _counter;

      @override
      void initState() {
        super.initState();
        storage = new CounterStorage(); // add this line
        storage.readCounter().then((int value) {
          setState(() {
            _counter = value;
          });
        });
      }

非常感谢。这个()示例在没有存储的情况下是如何工作的=new CounterStorage();如果在
main()
函数中注意到:
home:flatterdemo(存储:CounterStorage()),
CounterStorage类对象正在被传递。并且在flatterdemo的
状态类
,设置了
存储
变量来获取它。谢谢你的解释。
CounterStorage storage;
    int _counter;

      @override
      void initState() {
        super.initState();
        storage = new CounterStorage(); // add this line
        storage.readCounter().then((int value) {
          setState(() {
            _counter = value;
          });
        });
      }