Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Message when silver list is empty

How to show a message when SilverList is empty?
I am trying to filter a list of users using SliverList and SliverChildBuilderDelegate.
When I type some filter it returns empty screen, I want to show some message.
Here is my code

  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocBuilder<UsersCubit, UsersState>(builder: (context, userState) {
        if (userState is UsersLoaded) {
          List<UserEntity> allUsers = userState.users
              .where((user) =>
                  user.name!.contains(_searchController.text.toLowerCase()))
              .toList();

          return CustomScrollView(
            slivers: <Widget>[
              SliverAppBar(
                floating: true,
                snap: true,
                automaticallyImplyLeading: false,
                elevation: 0,
                title: TextField(
                  controller: _searchController,
                  style: Theme.of(context).textTheme.bodyText1,
                  decoration: InputDecoration(
                    hintText: 'Search ...',
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                    return User(
                      otherUser: allUsers[index],
                    );
                  },
                  childCount: allUsers.length,
                ),
              ),
            ],
          );
        } else {
          return const Scaffold(
              body: Center(child: CircularProgressIndicator()));
        }
      }),
    );
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Check the length of allUsers list and if it is empty return widget that shows message, otherwise return SliverList.

List<UserEntity> allUsers = userState.users
    .where((user) =>
        user.name!.contains(_searchController.text.toLowerCase()))
    .toList();

return CustomScrollView(
  slivers: <Widget>[
    SliverAppBar(
      floating: true,
      snap: true,
      automaticallyImplyLeading: false,
      elevation: 0,
      title: TextField(
        controller: _searchController,
        style: Theme.of(context).textTheme.bodyText1,
        decoration: InputDecoration(
          hintText: 'Search ...',
        ),
      ),
    ),
    if (allUsers.isEmpty) ...[
      SliverToBoxAdapter( 
          child: Container(
            // child widget to show when users is empty
          ),
      )
    ] else ...[
      SliverList(
        delegate: SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            return User(
              otherUser: allUsers[index],
            );
          },
          childCount: allUsers.length,
        ),
      ),
    ],
  ],
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading