networking_layer 1.1.2
networking_layer: ^1.1.2 copied to clipboard
A robust, solid-compliant networking layer using Dio.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:networking_layer/networking_layer.dart';
import 'package:dartz/dartz.dart' hide State;
void main() {
// 1. Centralized Initialization
AppConstants.init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Networking Layer Demo',
theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: true),
home: const DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
const DemoPage({super.key});
@override
State<DemoPage> createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
final _testApi = TestApi();
String _result = "Press a button to fetch data";
bool _isLoading = false;
void _updateResult(String text) => setState(() => _result = text);
// Example 1: Using Direct Style (Imperative)
Future<void> _fetchDirect() async {
setState(() => _isLoading = true);
try {
final users = await _testApi.getUsersDirect();
_updateResult("Direct Style Success: Found ${users.length} users");
} catch (e) {
_updateResult("Direct Style Error: $e");
} finally {
setState(() => _isLoading = false);
}
}
// Example 2: Using ApiClient Style (Functional)
Future<void> _fetchFunctional() async {
setState(() => _isLoading = true);
final result = await _testApi.getUserFunctional('1');
result.fold(
(error) => _updateResult("Functional Error: ${error.message}"),
(user) =>
_updateResult("Functional Success: User name is ${user['name']}"),
);
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Networking Layer Demo')),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_isLoading) const CircularProgressIndicator(),
const SizedBox(height: 20),
Text(
_result,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 16),
),
const SizedBox(height: 40),
ElevatedButton(
onPressed: _isLoading ? null : _fetchDirect,
child: const Text('Fetch Users (Direct Style)'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: _isLoading ? null : _fetchFunctional,
child: const Text('Fetch User #1 (Functional Style)'),
),
],
),
),
);
}
}
// Simple Repository combining both styles
class TestApi with ApiClient {
// Direct Style: Returns data directly or throws
Future<List<dynamic>> getUsersDirect() async {
HelperResponse response = await DioServices.instance.get('/users');
if (response.success) {
return response.data; // List<dynamic>
} else {
throw Exception(response.message);
}
}
// Functional Style: Returns Either<HelperResponse, T>
Future<Either<HelperResponse, Map<String, dynamic>>> getUserFunctional(
String id,
) async {
return await get(
'/users/$id',
fromJson: (json) => json as Map<String, dynamic>,
);
}
}
// Mock of main app constant settings
class AppConstants {
static void init() {
DioServices.instance.init(
DioConfig(
baseUrl: 'https://jsonplaceholder.typicode.com',
translate: (key) =>
"Translated: $key", // Link to your localization logic
),
);
}
}