Last active
          December 1, 2021 08:23 
        
      - 
      
- 
        Save riscait/e89c9e12fd4bceb939649fba47f1dc3f to your computer and use it in GitHub Desktop. 
  
    
      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'; | |
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
| class AsyncValueBuilder<R> extends StatelessWidget { | |
| const AsyncValueBuilder({ | |
| Key? key, | |
| required this.asyncValue, | |
| required this.builder, | |
| }) : super(key: key); | |
| final AsyncValue<R> asyncValue; | |
| final Widget Function(BuildContext context, R data) builder; | |
| @override | |
| Widget build(BuildContext context) { | |
| return asyncValue.when( | |
| data: (data) => builder(context, data), | |
| error: (error, stackTrace) => Center(child: Text('$error')), | |
| loading: () => const Center(child: CircularProgressIndicator.adaptive()), | |
| ); | |
| } | |
| } | |
| // Usage | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'AsyncValueBuilder Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: const MyHomePage(), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatelessWidget { | |
| const MyHomePage(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| body: Center( | |
| child: AsyncValueBuilder<String>( | |
| asyncValue: const AsyncValue.data('ABC'), | |
| builder: (context, value) { | |
| return Text(value); | |
| }, | |
| ), | |
| ), | |
| ); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment