loader 0.1.0 copy "loader: ^0.1.0" to clipboard
loader: ^0.1.0 copied to clipboard

outdated

run asynchronous code before building your widget. Loader will rebuild your widget after the loading is complete.

Loader #

Sometimes you need to load some data before building your widget. Because initState doesn't support asynchronous loading you need to find another way to load your data. The most common way of loading data is using a FutureBuilder but FutureBuilders are tedious. Another way is using flags to rebuild the widget after all the loading is done.

Loader uses the flag method.

LoadingMixin #

The LoadingMixin adds all the necessary flags to your stateful widget's state to turn it to a FutureBuilder like widget.

their are two flags:

  1. loading : true if the load function is still running
  2. hasError: true if the load function has thrown an exception.

the exception text is stored in the error variable.

class DataRow extends StatefulWidget {
  
  final int index;
  
  DataRow({Key key, this.index}) : super(key: key);
  
  @override
  _DataRowState createState() => _DataRowState();
}

class _DataRowState extends State<DataRow> with LoadingMixin<DataRow>{
  
  String data;
  
  @override
    Future<void> load() async{
      data = await DataProvider.getData();
    }
  
  @override
  Widget build(BuildContext context) {
    if(loading) return Container();
    if(hasError) return Text(error);
    return Text(data);
  }
}

Loader #

Loader is a widget which uses the LoadingMixin mixin.

This widget will run it's builder method, only after the load function is done.

The builder will get the value returned in the load function as the value parameter.

class Banner extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Loader<String>(
          load: () async{
            return await retriveBannerText();
          },
          builder: (context, value){
            return Text(value);
          },
          errorBuilder: (error) => Text(error, style: TextStyle(color: Colors.red),),
        ),
      ),
    );
  }
}

14
likes
0
pub points
61%
popularity

Publisher

verified publisheraligator.ir

run asynchronous code before building your widget. Loader will rebuild your widget after the loading is complete.

Homepage

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on loader