can we use FutureBuilder with Void type

1.8k Views Asked by At

my question is can we use FutureBuilder with Void callbacks on using such methods whome type is

Future<void> fun_name() async{ body }

after creating such methods how can we pass that method to FutureBuilder widget on whatever thy type is/.

1

There are 1 best solutions below

0
Ashwan tariq On

Yes you did't mention what you are trying to accomplish and why you wnna do it but here is demonstration.

Here is demo widget

class Test extends StatelessWidget {
  const Test({Key? key}) : super(key: key);
  Future<void> fun(){
  var s=   Future.delayed(Duration(seconds: 10));
    return s;
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder<void>(
        future: fun(),
        builder: (snap,context){
          return snap.widget; //return widget because builder's return type is widget
        },
      )
    );
  }
}

here i am doing it with async and await

class Test2 extends StatelessWidget {
  const Test2({Key? key}) : super(key: key);
  Future<void> delayedString() async {
    await Future.delayed(const Duration(seconds: 2));
    return;
  }
  @override
  Widget build(BuildContext context) {
    return Container(
        child: FutureBuilder<void>(
          future: delayedString(),
          builder: (snap,context){
            return snap.widget; //return widget because builder's return type is widget
          },
        )
    );
  }
}