Search code examples
flutterdartlistview

How can I show the ListTiles in a ListView


I am working on a leaderboard system where it sorts the map and it displays the keys on a listTile that's inside a listView, but when I reload the app, It won't show any listTiles here is the picture Any help is appreciated here is the code for this widget

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

Map players = {"Hello": 1468, "Ableflyer": 1820, "crazyman": 1536, "gottoosilly": 2160};
Map sortplayer = Map.fromEntries(players.entries.toList()..sort((e1, e2) => e1.value.compareTo(e2.value)));
class daily extends StatelessWidget {
  const daily({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ListView.builder(
          itemCount: sortplayer.length,
          itemBuilder: (BuildContext context, int index){
            return ListTile(
              title: Text(
                  sortplayer.keys.elementAt(index).toString()
              ),
            );
          },
        )
      ],
    );
  }
}

Solution

  • Wrap your ListView with Expanded widget.

    class daily extends StatelessWidget {
      const daily({Key? key}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return Column(
          children: [
            Expanded(
              child: ListView.builder(
                itemCount: sortplayer.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(
                    title: Text(sortplayer.keys.elementAt(index).toString()),
                  );
                },
              ),
            )
          ],
        );
      }
    }
    

    I will highly recommend checking this video