0

My apologies for what I assume is rather basic question, but I'm struggling to understand this. I'm aware of What is a Future and how do I use it? but I don't think that applies in this case, or if it does I'm even more confused than I thought!

I'm attempting to use FileOutput in the Logger package to log to device storage. This requires a File object as a parameter.

To obtain the correct path I'm using getApplicationDocumentsDirectory() from the path_provider package. This returns a Future which I can manipulate to a Future in an async function with await.

I'm unclear though how to extract a File from this and how to make sure that these objects are available to the logger before they are needed. Does this need to be done before I call runApp()? I assume I don't need to and shouldn't push async up to the main()?

This is where I am. x2() is a test function I can call successfully out of main() after invoking runApp() and gives me the correct results.

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  print("directory: $directory");

  return directory.path;
}

Future<File> get _localFile async {
  final path = await _localPath;
  print("path: $path");

  return File('$path/logger.out');
}

var logger2 = Logger(
  output: MultiOutput([
    ConsoleOutput(),
    FileOutput(
      file: _localFile,
      overrideExisting: true,
    ),
  ]),
  printer: PrettyPrinter(
    printBox: false,
    printTime: true,
  ),
  filter: ProductionFilter(),
);

void x2() async {
  var f = await _localFile;
  print("_localFile: $f");
}
Ian
  • 1,507
  • 3
  • 21
  • 36

2 Answers2

0

You should be able to set the Logger before the runApp but you don't need to await for it you can create a logger then access with a provider or a singleton instance and you can simple have a getLogger method that checks if the instance is available and if not calls the await overthere

this way you only call the future once and cache the instance in the class.

ahmetakil
  • 879
  • 6
  • 15
0

You may initialise your variable in x2() function. Also, don't forget to await x2() as it will assure you that your instance has been created.

class Logger{
  Logger({required this.number});
  int number;
}

Future<int> get someNumber => Future.delayed(Duration(seconds:1),()=>5);
 

var logger;
Future<void> x2() async {
  logger=Logger(number: await someNumber);
  print(logger.number);
}

void main()async{
   await x2();
   print("main :: "+ logger.number.toString());
}

edit: Also as @ahmetakil suggested, use provider or inherited widget if you need this instance down the widget tree