Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: examples/alice_dio #196

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions examples/alice_dio/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
#library_annotations: false
103 changes: 53 additions & 50 deletions examples/alice_dio/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,31 @@ import 'package:alice_dio/alice_dio_adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

void main() => runApp(MyApp());
void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
const MyApp({super.key});

@override
_MyAppState createState() => _MyAppState();
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
late Alice _alice;
late Dio _dio;
late AliceDioAdapter _aliceDioAdapter;

@override
void initState() {
_alice = Alice(
showNotification: true,
showInspectorOnShake: true,
maxCallsCount: 1000,
);
_aliceDioAdapter = AliceDioAdapter();
_dio = Dio(BaseOptions(
followRedirects: false,
));
late final AliceDioAdapter _aliceDioAdapter = AliceDioAdapter();

_dio.interceptors.add(_aliceDioAdapter);
_alice.addAdapter(_aliceDioAdapter);
late final Alice _alice = Alice(
showNotification: true,
showInspectorOnShake: true,
maxCallsCount: 1000,
)..addAdapter(_aliceDioAdapter);

super.initState();
}
late final Dio _dio = Dio(BaseOptions(followRedirects: false))
..interceptors.add(_aliceDioAdapter);

@override
Widget build(BuildContext context) {

return MaterialApp(
navigatorKey: _alice.getNavigatorKey(),
debugShowCheckedModeBanner: false,
Expand All @@ -47,28 +38,31 @@ class _MyAppState extends State<MyApp> {
title: const Text('Alice + Dio - Example'),
),
body: Container(
padding: EdgeInsets.all(16),
padding: const EdgeInsets.all(16),
child: ListView(
children: [
const SizedBox(height: 8),
Text(
'Welcome to example of Alice + Dio Example. Click buttons below to generate sample data.'),
const Text(
style: TextStyle(fontSize: 14),
'Welcome to example of Alice + Dio Example. '
'Click buttons below to generate sample data.',
),
ElevatedButton(
child: Text(
onPressed: _runDioRequests,
child: const Text(
'Run Dio HTTP Requests',
),
onPressed: _runDioRequests,
),

Text(
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,

)
],
),
Expand All @@ -77,57 +71,66 @@ class _MyAppState extends State<MyApp> {
);
}


void _runDioRequests() async {
Map<String, dynamic> body = <String, dynamic>{
final Map<String, dynamic> body = {
'title': 'foo',
'body': 'bar',
'userId': '1'
};
_dio.get<void>(
'https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org');
'https://httpbin.org/redirect-to?url=https%3A%2F%2Fhttpbin.org',
);
_dio.delete<void>('https://httpbin.org/status/500');
_dio.delete<void>('https://httpbin.org/status/400');
_dio.delete<void>('https://httpbin.org/status/300');
_dio.delete<void>('https://httpbin.org/status/200');
_dio.delete<void>('https://httpbin.org/status/100');
_dio.post<void>('https://jsonplaceholder.typicode.com/posts', data: body);
_dio.get<void>('https://jsonplaceholder.typicode.com/posts',
queryParameters: <String, dynamic>{'test': 1});
_dio.get<void>(
'https://jsonplaceholder.typicode.com/posts',
queryParameters: {'test': 1},
);
_dio.put<void>('https://jsonplaceholder.typicode.com/posts/1', data: body);
_dio.put<void>('https://jsonplaceholder.typicode.com/posts/1', data: body);
_dio.delete<void>('https://jsonplaceholder.typicode.com/posts/1');
_dio.get<void>('http://jsonplaceholder.typicode.com/test/test');

_dio.get<void>('https://jsonplaceholder.typicode.com/photos');
_dio.get<void>(
'https://icons.iconarchive.com/icons/paomedia/small-n-flat/256/sign-info-icon.png');
'https://icons.iconarchive.com/icons/paomedia/small-n-flat/256/sign-info-icon.png',
);
_dio.get<void>(
'https://images.unsplash.com/photo-1542736705-53f0131d1e98?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80');
'https://images.unsplash.com/photo-1542736705-53f0131d1e98?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80',
);
_dio.get<void>(
'https://findicons.com/files/icons/1322/world_of_aqua_5/128/bluetooth.png');
'https://findicons.com/files/icons/1322/world_of_aqua_5/128/bluetooth.png',
);
_dio.get<void>(
'https://upload.wikimedia.org/wikipedia/commons/4/4e/Pleiades_large.jpg');
'https://upload.wikimedia.org/wikipedia/commons/4/4e/Pleiades_large.jpg',
);
_dio.get<void>('http://techslides.com/demos/sample-videos/small.mp4');

_dio.get<void>('https://www.cse.wustl.edu/~jain/cis677-97/ftp/e_3dlc2.pdf');

final directory = await getApplicationDocumentsDirectory();
File file = File('${directory.path}/test.txt');
file.create();
file.writeAsStringSync('123456789');
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File(p.join(directory.path, 'test.txt'));
if (!await file.exists()) {
await file.create();
}
await file.writeAsString('123456789');

String fileName = file.path.split('/').last;
FormData formData = FormData.fromMap(<String, dynamic>{
final String fileName = file.path.split('/').last;
final FormData formData = FormData.fromMap({
'file': await MultipartFile.fromFile(file.path, filename: fileName),
});
_dio.post<void>('https://jsonplaceholder.typicode.com/photos',
data: formData);
_dio.post<void>(
'https://jsonplaceholder.typicode.com/photos',
data: formData,
);

_dio.get<void>('http://dummy.restapiexample.com/api/v1/employees');
}


void _runHttpInspector() {
_alice.showInspector();
}
Expand Down
5 changes: 5 additions & 0 deletions examples/alice_dio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ dependencies:
sdk: flutter
alice:
alice_dio:
path_provider: ^2.1.3
path: ^1.9.0
dio: ^5.4.3+1

dev_dependencies:
flutter_lints: ^4.0.0

dependency_overrides:
alice:
path: ../../packages/alice
Expand Down
Loading