简体   繁体   中英

How to show a text widget when a screen is not calling data from Firebase

I have the activity screen that gets data from Firebase, I'm trying to show a text message like "No activities yet" when the database is not providing data.

I tried to use a ternary operator condition that checks if the database is empty and it works, however, the problem is that when I go the activity screen and it has data, it still shows the message for about 1 second and then shows the activity screen, so, I would like to know if there is a better way to implement this.

Hope this makes sense.

See the code below

      @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: RefreshIndicator(
        onRefresh: () => _setupActivities(),
        child: _activities.isEmpty
            ? Center(
                child: Text(
                  'Aun no Tienes Notificaciones',
                  style: ktextTitlesStyle.copyWith(
                      color: Colors.black,
                      fontSize: responsive.height(context) / 50),
                ),
                )
            : ListView.builder(
                itemCount: _activities.length,
                itemBuilder: (BuildContext context, int index) {
                  Activity activity = _activities[index];
                  return _buildActivity(activity);
                },
              ),
      ),
    );
  }

}

I'm not sure I fully understand what you are trying to accomplish but checking for .hasData on the AsyncSnapshot might help determine if data is returned.

Container( child: FutureBuilder(future: someFutureFirebaseDataReturningMethod(), builder: (context, snapshot) { 
    if (!snapshot.hasData) return _noActiviesYet();
    return _showActivies(snapshot, context);
}))

.hasData returns "whether this snapshot contains a non-null data value."( https://api.flutter.dev/flutter/widgets/AsyncSnapshot/hasData.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM