fix error when token is null, add custom logger instance

This commit is contained in:
lukas 2021-12-11 13:33:46 +01:00
parent 209473daa8
commit d1f6f02fcc
5 changed files with 60 additions and 13 deletions

View File

@ -2,12 +2,12 @@ import 'dart:async';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:logger/logger.dart';
import '../log/log.dart';
class Token { class Token {
static final Token _token = Token._(); static final Token _token = Token._();
final _storage = const FlutterSecureStorage(); final _storage = const FlutterSecureStorage();
final _l = Logger();
String _tokenval = ""; String _tokenval = "";
@ -16,7 +16,7 @@ class Token {
} }
Future<String?> _readToken() async { Future<String?> _readToken() async {
_l.d("reading token store"); Log.d("reading token store");
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
return _storage.read(key: 'jwt'); return _storage.read(key: 'jwt');
} }
@ -26,7 +26,13 @@ class Token {
if (_tokenval == "") { if (_tokenval == "") {
final token = await _readToken(); final token = await _readToken();
// check if value is defined in phone store
if (token != null) {
completer.complete(token); completer.complete(token);
} else {
Log.d("no token defined");
completer.complete("");
}
} else { } else {
completer.complete(_tokenval); completer.complete(_tokenval);
} }

38
lib/log/log.dart Normal file
View File

@ -0,0 +1,38 @@
import 'package:logger/logger.dart';
class Log {
static final _verbolg = Logger();
static final _linelg = Logger(
printer: PrettyPrinter(methodCount: 0, printEmojis: false),
);
/// Log a message at level [Level.verbose].
static void v(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_linelg.log(Level.verbose, message, error, stackTrace);
}
/// Log a message at level [Level.debug].
static void d(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_linelg.log(Level.debug, message, error, stackTrace);
}
/// Log a message at level [Level.info].
static void i(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_linelg.log(Level.info, message, error, stackTrace);
}
/// Log a message at level [Level.warning].
static void w(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_verbolg.log(Level.warning, message, error, stackTrace);
}
/// Log a message at level [Level.error].
static void e(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_verbolg.log(Level.error, message, error, stackTrace);
}
/// Log a message at level [Level.wtf].
static void wtf(dynamic message, [dynamic error, StackTrace? stackTrace]) {
_verbolg.log(Level.wtf, message, error, stackTrace);
}
}

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import '../api/token.dart'; import '../api/token.dart';
import '../log/log.dart';
class LoginContainer extends StatefulWidget { class LoginContainer extends StatefulWidget {
const LoginContainer({Key? key, required this.child}) : super(key: key); const LoginContainer({Key? key, required this.child}) : super(key: key);
@ -23,8 +23,8 @@ class _LoginContainerState extends State<LoginContainer> {
final token = Token.getInstance(); final token = Token.getInstance();
token.getToken().then((value) { token.getToken().then((value) {
// todo this context call might occur before app rendered correctly! // todo this context call might occur before app rendered correctly!
Logger().i("The token value is $value"); Log.i("The token value is $value");
if (value != null && value != "") { if (value != "") {
setState(() { setState(() {
loggedIn = true; loggedIn = true;
loading = false; loading = false;
@ -43,8 +43,11 @@ class _LoginContainerState extends State<LoginContainer> {
return LoginContext( return LoginContext(
LoggedIn: loggedIn, LoggedIn: loggedIn,
child: loading child: loading
? const MaterialApp( ? MaterialApp(
home: Center(child: CircularProgressIndicator()), home: Container(
color: Colors.white,
child: const Center(child: CircularProgressIndicator()),
),
) )
: widget.child, : widget.child,
onLoggin: (bool login) { onLoggin: (bool login) {

View File

@ -1,11 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:openmediacentermobile/app.dart'; import 'package:openmediacentermobile/app.dart';
import 'log/log.dart';
import 'login/logincontext.dart'; import 'login/logincontext.dart';
// main app entry point // main app entry point
void main() { void main() {
Logger().i("App init!"); Log.i("App init!");
runApp(const LoginContainer(child: App())); runApp(const LoginContainer(child: App()));
} }

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'log/log.dart';
import 'preview_tile.dart'; import 'preview_tile.dart';
class VideoFeed extends StatelessWidget { class VideoFeed extends StatelessWidget {
@ -22,7 +22,7 @@ class VideoFeed extends StatelessWidget {
crossAxisSpacing: 10), crossAxisSpacing: 10),
itemCount: _vids.length, itemCount: _vids.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
print("item $index built!"); Log.d("item $index built!");
return PreviewTile(dta: _vids[index]); return PreviewTile(dta: _vids[index]);
}); });
} }