Home > Software engineering >  ListTile not showing when added to listview
ListTile not showing when added to listview

Time:01-20

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()
              ),
            );
          },
        )
      ],
    );
  }
}

CodePudding user response:

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

CodePudding user response:

Can you try to replace ListView.builder with this code

 ListView.builder(
  itemCount: sortplayer.length,
  itemBuilder: (BuildContext context, int index){
    var player = sortplayer.entries.toList()[index];
    return ListTile(
      title: Text(player.key),
    );
  },
)

Let me know if is this helpful to you

  • Related