cyber_req 2.0.12+2 copy "cyber_req: ^2.0.12+2" to clipboard
cyber_req: ^2.0.12+2 copied to clipboard

A flexible Flutter/Dart API client for backends with dynamic headers, secure token management, and comprehensive callbacks.

example/main.dart

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'example_code.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cyber Req Examples',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const ExampleScreen(),
    );
  }
}

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

  @override
  State<ExampleScreen> createState() => _ExampleScreenState();
}

class _ExampleScreenState extends State<ExampleScreen> {
  String _postResult = 'Loading...';
  String _getResult = 'Loading...';
  String _putResult = 'Loading...';
  String _deleteResult = 'Loading...';
  String _multipartResult = 'Loading...';
  String _tokenSaveResult = 'Loading...';
  String _tokenUseResult = 'Loading...';
  String _tokenClearResult = 'Loading...';
  String _errorHandlingResult = 'Loading...';

  @override
  void initState() {
    super.initState();
    _runAllExamples();
  }

  Future<void> _runAllExamples() async {
    // Example: POST Request
    setState(() => _postResult = 'Running POST example...');
    await createNewUser('John Doe', 'john.doe@example.com', 'password123');
    setState(() => _postResult = 'POST example finished. Check console for output.');

    // Example: GET Request
    setState(() => _getResult = 'Running GET example...');
    await fetchUserProfile('1'); // Assuming user ID 1
    setState(() => _getResult = 'GET example finished. Check console for output.');

    // Example: PUT Request
    setState(() => _putResult = 'Running PUT example...');
    await updateProduct('1', {'name': 'Updated Product', 'price': 99.99});
    setState(() => _putResult = 'PUT example finished. Check console for output.');

    // Example: DELETE Request
    setState(() => _deleteResult = 'Running DELETE example...');
    await removeComment('1'); // Assuming comment ID 1
    setState(() => _deleteResult = 'DELETE example finished. Check console for output.');

    // Example: Multipart POST Request (requires a dummy file)
    setState(() => _multipartResult = 'Running Multipart POST example...');
    // Create a dummy file for upload
    final directory = await getTemporaryDirectory();
    final dummyFile = File('${directory.path}/dummy_image.jpg');
    if (!await dummyFile.exists()) {
      await dummyFile.writeAsBytes([0, 1, 2, 3]); // Write some dummy bytes
    }
    await uploadProfilePicture('1', dummyFile);
    setState(() => _multipartResult = 'Multipart POST example finished. Check console for output.');

    // Example: Token Management
    setState(() => _tokenSaveResult = 'Running Token Save example...');
    await saveAuthToken('my_super_secret_token');
    setState(() => _tokenSaveResult = 'Token Save example finished. Check console for output.');

    setState(() => _tokenUseResult = 'Running Token Use example...');
    // This would typically be part of another request, but for demonstration:
    // await apiService.get('protected-data', useStorageToken: true);
    setState(() => _tokenUseResult = 'Token Use example finished. Check console for output.');

    setState(() => _tokenClearResult = 'Running Token Clear example...');
    await clearAuthToken();
    setState(() => _tokenClearResult = 'Token Clear example finished. Check console for output.');

    // Example: Error Handling
    setState(() => _errorHandlingResult = 'Running Error Handling example...');
    await demonstrateErrorHandling();
    setState(() => _errorHandlingResult = 'Error Handling example finished. Check console for output.');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Cyber Req Examples'),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('POST Example: $_postResult'),
            const SizedBox(height: 10),
            Text('GET Example: $_getResult'),
            const SizedBox(height: 10),
            Text('PUT Example: $_putResult'),
            const SizedBox(height: 10),
            Text('DELETE Example: $_deleteResult'),
            const SizedBox(height: 10),
            Text('Multipart POST Example: $_multipartResult'),
            const SizedBox(height: 10),
            Text('Token Save Example: $_tokenSaveResult'),
            const SizedBox(height: 10),
            Text('Token Use Example: $_tokenUseResult'),
            const SizedBox(height: 10),
            Text('Token Clear Example: $_tokenClearResult'),
            const SizedBox(height: 10),
            Text('Error Handling Example: $_errorHandlingResult'),
          ],
        ),
      ),
    );
  }
}
2
likes
0
points
39
downloads

Publisher

unverified uploader

Weekly Downloads

A flexible Flutter/Dart API client for backends with dynamic headers, secure token management, and comprehensive callbacks.

Homepage

License

unknown (license)

Dependencies

flutter, flutter_secure_storage, http

More

Packages that depend on cyber_req