skip to Main Content

I have this code that will get a list of reminders in DB and show it on the screen. It works fine with the reminder list. The problem is when the list is empty (there is no reminder), i want it to show some text like "There is no reminder". So i tried with Column, Text widget, i wrapped them in a list like below but nothing shown on screen.

If i remove the [], it is error A value of type ‘Card’ can’t be returned from the method ‘buildReminderList’ because it has a return type of ‘List’.

Does anyone know how to make a text like "There is no reminder" show in the case when "Upcoming" list is empty.

List<Widget> buildReminderList(List<Reminder> reminders) {
    debugPrint(reminders.length.toString());
    if (reminders != null)
      return reminders.map((Reminder reminder)
      => ListTile()).toList();
    else
      return [Card(
          child: ListTile(
            title: Text("There is no reminder", style: TextStyle(fontWeight: FontWeight.bold)),
          )
      )];
  }

enter image description here

2

Answers


  1. Simply replace List<Widget> with Widget:-

    Widget buildReminderList(List<Reminder> reminders) {
        debugPrint(reminders.length.toString());
        if (reminders != null)
          return reminders.map((Reminder reminder)
          => ListTile()).toList();
        else
          return Text("There is no reminder", style: TextStyle(fontWeight: FontWeight.bold));
      }
    
    Login or Signup to reply.
  2. Result

    output

    try this way

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    
    class Test extends StatelessWidget {
      List<Widget> buildReminderList() {
        return [
          Card(
            child: ListTile(
              title: Text("There is no reminder",
                  style: TextStyle(fontWeight: FontWeight.bold)),
            ),
          ),
        ];
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ListView(
              children: [
                ...buildReminderList().toList(),
              ],
            ),
          ),
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search