update example app.set logger data listenable.

master
小赖 4 years ago
parent cc42a73919
commit 97476abb35

@ -1,5 +1,10 @@
# CHANGELOG
## [0.1.3]
* listenable value.
* update example app.
## [0.1.2]
* performance enhanced.

@ -9,10 +9,7 @@ A well log tool on flutter, support Dio and custom type data.
* init logger
```dart
Future.delayed(
Duration(milliseconds: 300),
() => PowerLogger.init(context),
);
PowerLogger.start(context);
```
* add data to logger

@ -28,29 +28,17 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
Dio().get("https://192.168.31.255:8888").catchError((e) {
LoggerData.addData(e);
});
Dio().get("https://www.baidu.com", queryParameters: {'test': 'test'}).then(
(e) {
LoggerData.addData(e);
});
Dio().get("https://www.baidu.com/ahefbawfbe.html").catchError((e) {
LoggerData.addData(e);
});
Dio()
.post("https://www.baidu.com/ahefbawfbe.html",
data: FormData.fromMap({'test': 'test'}))
.catchError((e) {
LoggerData.addData(e);
});
NetTool.init();
NetTool.dio.get("https://192.168.31.255:8888");
NetTool.dio.get("https://www.baidu.com", queryParameters: {'test': 'test'});
NetTool.dio.get("https://www.baidu.com/ahefbawfbe.html");
NetTool.dio.post(
"https://www.baidu.com/ahefbawfbe.html",
data: FormData.fromMap({'test': 'test'}),
);
Dio()
.get(
"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png")
.then((e) {
LoggerData.addData(e);
});
NetTool.dio.get(
"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
PowerLogger.start(context);
LoggerData.addData('TEST');
}
@ -64,3 +52,22 @@ class _MyHomePageState extends State<MyHomePage> {
);
}
}
class NetTool {
static Dio dio = Dio();
static init() {
dio.interceptors.add(InterceptorsWrapper(
onRequest: (options) async {
return options;
},
onResponse: (response) async {
LoggerData.addData(response);
return response;
},
onError: (DioError e) async {
LoggerData.addData(e);
return e;
},
));
}
}

@ -1,13 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:power_logger/power_logger.dart';
/// logger data storage
class LoggerData {
static int _maxLength = 100;
static List<dynamic> _data = [];
/// the real logger data.
static List<dynamic> get data => _data;
static List<dynamic> get data => _listenableData.value;
static _LoggerDataNotifer<List<dynamic>> _listenableData =
_LoggerDataNotifer([]);
/// set the logger max number.
static setMax(int max) {
@ -17,15 +19,32 @@ class LoggerData {
/// add data to logger
static addData(dynamic data) {
if (PowerLogger.debug) {
if (_data.length < _maxLength)
_data.insert(0, data);
else {
_data.removeLast();
_data.insert(0, data);
if (_listenableData.value.length < _maxLength) {
_listenableData.value.insert(0, data);
_listenableData.notify();
} else {
_listenableData.value.removeLast();
_listenableData.value.insert(0, data);
_listenableData.notify();
}
}
}
/// clear all logger
static clear() => _data.clear();
static clear() => _listenableData.value.clear();
/// add data listener
static addListener(Function listener) {
_listenableData.addListener(listener);
}
/// remove data listener
static removeListener(Function listener) {
_listenableData.removeListener(listener);
}
}
class _LoggerDataNotifer<T> extends ValueNotifier {
_LoggerDataNotifer(value) : super(value);
notify() => this.notifyListeners();
}

@ -1,5 +1,3 @@
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:power_logger/src/widgets/logger_fab.dart';

@ -132,14 +132,14 @@ class _DioResponseViewState extends State<DioResponseView> {
);
}
_buildSingleText(String title, String data) {
return data == null || data.length == 0
? SizedBox()
: BoxView(
title: Text('Params'),
child: SelectableText(data.toString()),
);
}
// _buildSingleText(String title, String data) {
// return data == null || data.length == 0
// ? SizedBox()
// : BoxView(
// title: Text('Params'),
// child: SelectableText(data.toString()),
// );
// }
@override
void initState() {

@ -10,6 +10,20 @@ class LoggerView extends StatefulWidget {
}
class _LoggerViewState extends State<LoggerView> {
updateList() => setState(() {});
@override
void initState() {
super.initState();
LoggerData.addListener(updateList);
}
@override
void dispose() {
LoggerData.removeListener(updateList);
super.dispose();
}
@override
Widget build(BuildContext context) {
return ListView.builder(

Loading…
Cancel
Save