Last active
March 27, 2024 19:41
-
-
Save Mastersam07/65b822ea7bf3bc87b099d1fec59c7745 to your computer and use it in GitHub Desktop.
Future controller idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| typedef FutureGenerator<T> = Future<T> Function(); | |
| class FutureController<T> { | |
| Future<T>? _future; | |
| VoidCallback? _onUpdate; | |
| final FutureGenerator<T>? _futureGenerator; | |
| FutureController({FutureGenerator<T>? futureGenerator}) | |
| : _futureGenerator = futureGenerator { | |
| // Generate the initial future if a generator is provided | |
| if (_futureGenerator != null) { | |
| _future = _futureGenerator(); | |
| } | |
| } | |
| Future<T>? get future => _future; | |
| void refresh() { | |
| if (_futureGenerator != null) { | |
| _future = _futureGenerator(); | |
| _notifyListeners(); | |
| } | |
| } | |
| void _notifyListeners() { | |
| _onUpdate?.call(); | |
| } | |
| void addListener(VoidCallback onUpdate) { | |
| _onUpdate = onUpdate; | |
| // If the future is not set, generate and notify listeners immediately. | |
| if (_future == null && _futureGenerator != null) { | |
| _future = _futureGenerator(); | |
| _notifyListeners(); | |
| } | |
| } | |
| } | |
| void main() { | |
| runApp(const MyApp()); | |
| } | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| debugShowCheckedModeBanner: false, | |
| home: MyFutureBuilderWidget(), | |
| ); | |
| } | |
| } | |
| class MyFutureBuilderWidget extends StatefulWidget { | |
| @override | |
| _MyFutureBuilderWidgetState createState() => _MyFutureBuilderWidgetState(); | |
| } | |
| class _MyFutureBuilderWidgetState extends State<MyFutureBuilderWidget> { | |
| late FutureController<String> futureController; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| futureController = FutureController<String>( | |
| futureGenerator: fetchData, // Provide the future generator | |
| ); | |
| // Add listener to rebuild when future updates | |
| futureController.addListener(() { | |
| setState(() {}); | |
| }); | |
| } | |
| Future<String> fetchData() async { | |
| await Future.delayed(Duration(seconds: 5)); | |
| return 'Data Loaded'; | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar(title: Text('FutureController with FutureBuilder')), | |
| body: FutureBuilder<String>( | |
| future: futureController.future, | |
| builder: (context, snapshot) { | |
| if (snapshot.connectionState == ConnectionState.waiting) { | |
| return Center(child: CircularProgressIndicator()); | |
| } | |
| if (snapshot.hasError) { | |
| return Center(child: Text('Error: ${snapshot.error}')); | |
| } | |
| return Center(child: Text(snapshot.data ?? 'No data')); | |
| }, | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () => futureController.refresh(), | |
| child: Icon(Icons.refresh), | |
| ), | |
| ); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using a ValueNotifier