Kyung uk Kim
Kyung uk Kim

Reputation: 5

Flutter : Wait for receiving data

  body: dataList.length == 0
      ? DelayedDisplay(
    delay: Duration(seconds: 2),
    child: Center(
      child:Text(
      "No Data Found",
      textAlign: TextAlign.center,
    ),)
  )
      : RefreshIndicator(
    onRefresh: () {
      Navigator.pushReplacement(context, MaterialPageRoute(
      builder: (context) => HomeScreen()));
      return Future.value(true);
      },
      child: ListView.builder(
        padding: EdgeInsets.only(bottom: 100),
        itemCount: dataList.length,
        itemBuilder: (_, index) {
          return cardUI(......);
        },
    ),
  ),

My app needs some time to get data from firebase.

So I tried giving a delay before displaying "No data", but there are still cases where "No data" is displayed first.

I tried using Timer and Future.delayed, but I got the error "...is not a subtype of type widget".

Can you please let me know if you have any ideas?

Upvotes: 0

Views: 432

Answers (1)

encubos
encubos

Reputation: 3283

Using a DelayedDisplay is not necessary.

I think you should use a FutureBuilder in this case.

https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

Read the documentation in that link. Also take a look to the video!

Also you could take a look at this code. It uses the FutureBuilder widget and the same concept of building your UI depending on a function that return someData in the Future.

 Future<String> someFutureStringFunction() async {
    return Future.delayed(const Duration(seconds: 1), () => "someText");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: someFutureStringFunction(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Text(snapshot.data);
          } else {
            return Text('Loading...');
          }
        },
      ),
    );
  }

Upvotes: 1

Related Questions