Created
January 11, 2020 07:17
-
-
Save eGust/b49acc3bbbc954d209182082c2ed7dd2 to your computer and use it in GitHub Desktop.
flutter_cache_store test boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:io'; | |
import 'dart:developer'; | |
const HEADERS = { | |
'Content-Type': 'application/json', | |
'Cache-Control': 'max-age=120', | |
}; | |
const PORT = 2020; | |
class EchoHttpServer { | |
static HttpServer _server; | |
HttpServer get server => _server; | |
Future<void> echo(HttpRequest req) async { | |
final res = req.response; | |
HEADERS.forEach((name, value) { | |
res.headers.add(name, value); | |
}); | |
final uri = req.requestedUri; | |
final ts = DateTime.now().toIso8601String(); | |
final params = uri.queryParameters; | |
log('[ECHO $ts] $uri'); | |
final data = { | |
'path': uri.path, | |
'query': params, | |
'ts': ts, | |
}; | |
final size = int.tryParse(params['size'] ?? '0'); | |
if (size != null && size > 0) { | |
data['payload'] = 'x' * (size * 1000); | |
} | |
res.write(json.encode(data)); | |
res.close(); | |
} | |
Future<void> initialize() async { | |
_server = await HttpServer.bind(InternetAddress.anyIPv4, PORT); | |
_server.listen(echo); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:developer'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_cache_store/flutter_cache_store.dart'; | |
import 'echo_http_server.dart'; | |
CacheStore store; | |
void main() async { | |
final server = EchoHttpServer(); | |
server.initialize(); | |
runApp(MyApp()); | |
} | |
const REQUESTS = [ | |
'http://localhost:$PORT/foo?foo=foo&size=3', | |
'http://localhost:$PORT/bar?bar=bar', | |
'http://localhost:$PORT/baz?baz=baz&size=20', | |
'http://localhost:$PORT/foo_bar?foo_bar=foo_bar', | |
'http://localhost:$PORT/foo_baz?foo_baz=foo_baz&size=100', | |
'http://localhost:$PORT/bar_baz?bar_baz=bar_baz', | |
]; | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
String _json = ''; | |
_MyHomePageState() { | |
initStore(); | |
} | |
Future<void> initStore() async { | |
if (store != null) return; | |
store = await CacheStore.getInstance( | |
policy: CacheControlPolicy( | |
maxCount: 3, | |
maxAge: const Duration(seconds: 100), | |
), | |
); | |
print('path ${store.path}'); | |
} | |
void _incrementCounter() async { | |
final url = REQUESTS[_counter % REQUESTS.length]; | |
final ts = DateTime.now().toIso8601String(); | |
log('[FETCH $ts] $url'); | |
final file = await store.getFile(url); | |
final data = file.readAsStringSync(); | |
final timestamp = json.decode(data)['ts']; | |
log('[RESPONSE] size=${data.length} timestamp=$timestamp'); | |
setState(() { | |
_counter++; | |
_json = data.length > 1000 | |
? '(size=${data.length} timestamp=$timestamp)' | |
: data; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'You have pushed the button this many times: $_counter', | |
), | |
Text('JSON: $_json'), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment