yunxin_alog 1.0.8-rc.1 yunxin_alog: ^1.0.8-rc.1 copied to clipboard
Yun Xin Alog for flutter.
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:yunxin_alog/yunxin_alog.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;
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');
}
}
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._localPath.then((value) => print(
'init: $value,${Alog.init(ALogLevel.verbose, value, 'flog')}'));
widget.storage.readCounter().then((int value) {
setState(() {
_counter = value;
});
});
}
Future<File> _incrementCounter() {
setState(() {
_counter++;
});
var startTime = currentTimeMillis();
print('start time: $startTime');
for (var i = 0; i < 100*100; i++) {
Alog.v(tag: '111', content: '_counter$_counter');
// Alog.d(tag: '111', log: '_counter$_counter');
}
var pt = currentTimeMillis() - startTime;
print('use time: $pt');
// Write the variable as a string to the file.
return widget.storage.writeCounter(_counter);
}
static int currentTimeMillis() {
return new DateTime.now().millisecondsSinceEpoch;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Reading and Writing Files')),
body: Center(
child: Text(
'Button tapped $_counter time${_counter == 1 ? '' : 's'}.',
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}