From 39506e58368da08d7a704059650d7882fd8a73a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klemen=20Tu=C5=A1ar?= Date: Sun, 16 Jun 2024 10:15:50 +0100 Subject: [PATCH] refactor: examples/alice_http --- examples/alice_http/analysis_options.yaml | 22 ++- examples/alice_http/lib/main.dart | 168 +++++++++++----------- examples/alice_http/pubspec.yaml | 3 + 3 files changed, 95 insertions(+), 98 deletions(-) diff --git a/examples/alice_http/analysis_options.yaml b/examples/alice_http/analysis_options.yaml index 01f89e9c..d4e71cb5 100644 --- a/examples/alice_http/analysis_options.yaml +++ b/examples/alice_http/analysis_options.yaml @@ -1,16 +1,12 @@ -1include: package:very_good_analysis/analysis_options.yaml +analyzer: + exclude: + - "lib/generated_plugin_registrant.dart" + - "**.g.dart" + - "**.mocks.dart" + - "**.gen.dart" + +include: package:flutter_lints/flutter.yaml linter: rules: - public_member_api_docs: false - flutter_style_todos: false - avoid_final_parameters: false - sort_constructors_first: false - avoid_function_literals_in_foreach_calls: false - avoid_positional_boolean_parameters: false - use_if_null_to_convert_nulls_to_bools: false - use_build_context_synchronously: false - prefer_constructors_over_static_methods: false - use_setters_to_change_properties: false - avoid_print: false - sort_pub_dependencies: false \ No newline at end of file + #library_annotations: false diff --git a/examples/alice_http/lib/main.dart b/examples/alice_http/lib/main.dart index d7a2742c..0b53deda 100644 --- a/examples/alice_http/lib/main.dart +++ b/examples/alice_http/lib/main.dart @@ -1,38 +1,29 @@ - import 'package:alice/alice.dart'; import 'package:alice_http/alice_http_adapter.dart'; import 'package:alice_http/alice_http_extensions.dart'; - import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; -void main() => runApp(MyApp()); +void main() => runApp(const MyApp()); class MyApp extends StatefulWidget { + const MyApp({super.key}); + @override - _MyAppState createState() => _MyAppState(); + State createState() => _MyAppState(); } class _MyAppState extends State { - late Alice _alice; - late AliceHttpAdapter _aliceHttpAdapter; + late final AliceHttpAdapter _aliceHttpAdapter = AliceHttpAdapter(); - @override - void initState() { - _alice = Alice( - showNotification: true, - showInspectorOnShake: true, - maxCallsCount: 1000, - ); - _aliceHttpAdapter = AliceHttpAdapter(); - _alice.addAdapter(_aliceHttpAdapter); - - super.initState(); - } + late final Alice _alice = Alice( + showNotification: true, + showInspectorOnShake: true, + maxCallsCount: 1000, + )..addAdapter(_aliceHttpAdapter); @override Widget build(BuildContext context) { - return MaterialApp( navigatorKey: _alice.getNavigatorKey(), debugShowCheckedModeBanner: false, @@ -41,25 +32,33 @@ class _MyAppState extends State { title: const Text('Alice + HTTP package - Example'), ), body: Container( - padding: EdgeInsets.all(16), + padding: const EdgeInsets.all(16), child: ListView( children: [ const SizedBox(height: 8), - _getTextWidget( - 'Welcome to example of Alice Http Inspector. Click buttons below to generate sample data.'), - + const Text( + style: TextStyle(fontSize: 14), + 'Welcome to example of Alice Http Inspector. ' + 'Click buttons below to generate sample data.', + ), ElevatedButton( - child: Text( + onPressed: _runHttpHttpRequests, + child: const Text( 'Run http/http HTTP Requests', ), - onPressed: _runHttpHttpRequests, ), - + const SizedBox(height: 8), + const Text( + style: TextStyle(fontSize: 14), + 'After clicking on buttons above, you should receive notification.' + ' Click on it to show inspector. ' + 'You can also shake your device or click button below.', + ), ElevatedButton( - child: Text( + onPressed: _runHttpInspector, + child: const Text( 'Run HTTP Inspector', ), - onPressed: _runHttpInspector, ) ], ), @@ -68,107 +67,106 @@ class _MyAppState extends State { ); } - Widget _getTextWidget(String text) { - return Text( - text, - style: TextStyle(fontSize: 14), - textAlign: TextAlign.center, - ); - } - - void _runHttpHttpRequests() async { - Map body = { + final Map body = { 'title': 'foo', 'body': 'bar', 'userId': '1' }; + http - .post(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!, - body: body) + .post(Uri.https('jsonplaceholder.typicode.com', '/posts'), body: body) .interceptWithAlice(_aliceHttpAdapter, body: body); http - .get(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!) + .get(Uri.https('jsonplaceholder.typicode.com', '/posts')) .interceptWithAlice(_aliceHttpAdapter); http - .put(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!, - body: body) + .put(Uri.https('jsonplaceholder.typicode.com', '/posts/1'), body: body) .interceptWithAlice(_aliceHttpAdapter, body: body); http - .patch(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!, - body: body) + .patch( + Uri.https('jsonplaceholder.typicode.com', '/posts/1'), + body: body, + ) .interceptWithAlice(_aliceHttpAdapter, body: body); http - .delete(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!) + .delete(Uri.https('jsonplaceholder.typicode.com', '/posts/1')) .interceptWithAlice(_aliceHttpAdapter, body: body); http - .get(Uri.tryParse('https://jsonplaceholder.typicode.com/test/test')!) + .get(Uri.https('jsonplaceholder.typicode.com', '/test/test')) .interceptWithAlice(_aliceHttpAdapter); http - .post(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!, - body: body) - .then((response) { - _aliceHttpAdapter.onResponse(response, body: body); - }); + .post(Uri.https('jsonplaceholder.typicode.com', '/posts'), body: body) + .then((response) => _aliceHttpAdapter.onResponse(response, body: body)); http - .get(Uri.tryParse('https://jsonplaceholder.typicode.com/posts')!) - .then((response) { - _aliceHttpAdapter.onResponse(response); - }); + .get(Uri.https('jsonplaceholder.typicode.com', '/posts')) + .then((response) => _aliceHttpAdapter.onResponse(response)); http - .put(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!, - body: body) - .then((response) { - _aliceHttpAdapter.onResponse(response, body: body); - }); + .put(Uri.https('jsonplaceholder.typicode.com', '/posts/1'), body: body) + .then((response) => _aliceHttpAdapter.onResponse(response, body: body)); http - .patch(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!, - body: body) - .then((response) { - _aliceHttpAdapter.onResponse(response, body: body); - }); + .patch( + Uri.https('jsonplaceholder.typicode.com', '/posts/1'), + body: body, + ) + .then((response) => _aliceHttpAdapter.onResponse(response, body: body)); http - .delete(Uri.tryParse('https://jsonplaceholder.typicode.com/posts/1')!) - .then((response) { - _aliceHttpAdapter.onResponse(response); - }); + .delete(Uri.https('jsonplaceholder.typicode.com', '/posts/1')) + .then((response) => _aliceHttpAdapter.onResponse(response)); http - .get(Uri.tryParse('https://jsonplaceholder.typicode.com/test/test')!) - .then((response) { - _aliceHttpAdapter.onResponse(response); - }); + .get(Uri.https('jsonplaceholder.typicode.com', '/test/test')) + .then((response) => _aliceHttpAdapter.onResponse(response)); http .post( - Uri.tryParse( - 'https://jsonplaceholder.typicode.com/posts?key1=value1')!, - body: body) + Uri.https( + 'jsonplaceholder.typicode.com', + '/posts', + {'key1': 'value1'}, + ), + body: body, + ) .interceptWithAlice(_aliceHttpAdapter, body: body); http .post( - Uri.tryParse( - 'https://jsonplaceholder.typicode.com/posts?key1=value1&key2=value2&key3=value3')!, - body: body) + Uri.https( + 'jsonplaceholder.typicode.com', + '/posts', + { + 'key1': 'value1', + 'key2': 'value2', + 'key3': 'value3', + }, + ), + body: body, + ) .interceptWithAlice(_aliceHttpAdapter, body: body); http - .get(Uri.tryParse( - 'https://jsonplaceholder.typicode.com/test/test?key1=value1&key2=value2&key3=value3')!) - .then((response) { - _aliceHttpAdapter.onResponse(response); - }); + .get( + Uri.https( + 'jsonplaceholder.typicode.com', + '/test/test', + { + 'key1': 'value1', + 'key2': 'value2', + 'key3': 'value3', + }, + ), + ) + .then((response) => _aliceHttpAdapter.onResponse(response)); } void _runHttpInspector() { diff --git a/examples/alice_http/pubspec.yaml b/examples/alice_http/pubspec.yaml index d747dff9..0ce6bae0 100644 --- a/examples/alice_http/pubspec.yaml +++ b/examples/alice_http/pubspec.yaml @@ -14,6 +14,9 @@ dependencies: alice_http: http: ^1.2.1 +dev_dependencies: + flutter_lints: ^4.0.0 + dependency_overrides: alice: path: ../../packages/alice