Riyazat Durrani
Riyazat Durrani

Reputation: 668

Future Builder malfunctioning in flutter

i was writing my future builder like this

 FutureBuilder(
        future: Future.delayed(Duration(seconds: 3)),
        builder: (c, s) =>
        s.connectionState != ConnectionState.done
            ? Center(child: CircularProgressIndicator())
            :  nextPage()

ALthough working fine, but it was showing an error for couple of seconds which said "type 'Future' is not a subtype of type 'Widget' is thrown my futureBuilder. " next i changed it to

    FutureBuilder(
  future: Future.delayed(Duration(seconds: 3)).then((response) {
    nextPage();    
  }),  
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      return null;
    } else if (snapshot.hasError)
      return Text(snapshot.error.toString());
    return CircularProgressIndicator();
  },
);

but here the issue is my nextpage() just flashes and vanishes away.

any help?

EDIT: here is my full code, and my requirement is a circularprogressindicator for first 3 seconds,and then nextpage().

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(App());
}
class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Title',
      home: MyApp(),
    );
  }
}


class MyApp extends StatefulWidget {

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {









  nextPage() async{

    bool visitedFlag = await getVIsitingFlag();
    setVIsitingFlag();
    if (visitedFlag == true) {
      Navigator.of(context).push(
          MaterialPageRoute(builder: (context) => HomeScreen()));
    }
    else {
      Navigator.of(context).push(
          MaterialPageRoute(builder: (context) => NewScreen()));
    }
  }


@override
  void initState() {
  Future.delayed(Duration(seconds: 3)).then((response) {
    nextPage();
  });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFF311a2e),
      body: SafeArea(
        child:
     CircularProgressIndicator();
    ),
    );
  }
}



 getVIsitingFlag() async{
SharedPreferences preferences = await SharedPreferences.getInstance();
bool alreadyVisited = preferences.getBool('alreadyVisited') ?? false ;
return alreadyVisited;
}

 setVIsitingFlag()async{
  SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setBool('alreadyVisited', true);
}

Upvotes: 1

Views: 126

Answers (1)

nvoigt
nvoigt

Reputation: 77285

Since you don't want to show any widget on completion of your future anyway, just remove the FutureBuilder. You only need it when you want to build something based on your future and you don't.

Call your method in your initState method:

  Future.delayed(Duration(seconds: 3)).then((response) {
    nextPage();    
  }

and build your page to look like a progress indicator, without the FutureBuilder.

Upvotes: 1

Related Questions