shared_prefs_telemetry 0.1.1 copy "shared_prefs_telemetry: ^0.1.1" to clipboard
shared_prefs_telemetry: ^0.1.1 copied to clipboard

A Flutter package that automatically reads SharedPreferences data and sends it to a telemetry server at app launch.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_prefs_telemetry/shared_prefs_telemetry.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Example 1: Basic configuration without Firebase
  // await SharedPrefsTelemetry.initialize(
  //   TelemetryConfig(
  //     serverUrl: 'https://your-telemetry-server.com/api/collect',
  //     apiKey: 'your-api-key-here',
  //     enableLogging: true,
  //     excludeKeys: ['sensitive_password', 'user_token'],
  //     timeout: const Duration(seconds: 15),
  //   ),
  // );

  // Example 2: Configuration with Firebase Remote Config
  await SharedPrefsTelemetry.initialize(
    TelemetryConfig(
      serverUrl: 'https://fallback-telemetry-server.com/api/collect', // Fallback URL
      apiKey: 'your-api-key-here',
      enableLogging: true,
      excludeKeys: ['sensitive_password', 'user_token'],
      timeout: const Duration(seconds: 15),
      useFirebaseRemoteConfig: true,
      firebaseRemoteConfigKey: 'telemetry_server_url', // Key in Firebase Remote Config
    ),
  );
  
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'SharedPrefs Telemetry Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'SharedPrefs Telemetry Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _keyController = TextEditingController();
  final TextEditingController _valueController = TextEditingController();
  List<String> _storedKeys = [];

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

  Future<void> _loadStoredKeys() async {
    final prefs = await SharedPreferences.getInstance();
    setState(() {
      _storedKeys = prefs.getKeys().toList();
    });
  }

  Future<void> _savePreference() async {
    if (_keyController.text.isEmpty || _valueController.text.isEmpty) {
      return;
    }

    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_keyController.text, _valueController.text);
    
    _keyController.clear();
    _valueController.clear();
    
    await _loadStoredKeys();
    
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text('Preference saved! Telemetry data will be sent automatically.'),
      ),
    );
  }

  Future<void> _clearAllPreferences() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.clear();
    await _loadStoredKeys();
    
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('All preferences cleared!')),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            const Text(
              'This app demonstrates the SharedPrefs Telemetry package with Firebase Remote Config.\nSharedPreferences data is automatically sent to a telemetry server at app launch.\nThe server URL can be dynamically configured via Firebase Remote Config.',
              style: TextStyle(fontSize: 16),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 20),
            TextField(
              controller: _keyController,
              decoration: const InputDecoration(
                labelText: 'Preference Key',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 10),
            TextField(
              controller: _valueController,
              decoration: const InputDecoration(
                labelText: 'Preference Value',
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  onPressed: _savePreference,
                  child: const Text('Save Preference'),
                ),
                ElevatedButton(
                  onPressed: _clearAllPreferences,
                  style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.red,
                    foregroundColor: Colors.white,
                  ),
                  child: const Text('Clear All'),
                ),
              ],
            ),
            const SizedBox(height: 20),
            const Text(
              'Stored Preferences:',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 10),
            Expanded(
              child: ListView.builder(
                itemCount: _storedKeys.length,
                itemBuilder: (context, index) {
                  final key = _storedKeys[index];
                  return FutureBuilder<String?>(
                    future: SharedPreferences.getInstance()
                        .then((prefs) => prefs.getString(key)),
                    builder: (context, snapshot) {
                      return ListTile(
                        title: Text(key),
                        subtitle: Text(snapshot.data ?? 'Loading...'),
                      );
                    },
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
125
points
45
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package that automatically reads SharedPreferences data and sends it to a telemetry server at app launch.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

firebase_core, firebase_remote_config, flutter, http, shared_preferences

More

Packages that depend on shared_prefs_telemetry