Home > Software engineering >  How to fix that ToString() gives out error with an int?
How to fix that ToString() gives out error with an int?

Time:07-21

I got this error when I'm trying to add a int to the List: "Too many positional arguments: 0 expected, but 1 found. Try removing the extra positional arguments, or specifying the name for named arguments"

leaderboard.dart:

class LeaderBoard extends StatefulWidget {
  const LeaderBoard({Key? key}) : super(key: key);

  @override
  State<LeaderBoard> createState() => _LeaderBoard();
}

class _LeaderBoard extends State<LeaderBoard> {
  @override
  Widget build(BuildContext context) {
    List<Widget> _textList = [];
    final board = Provider.of<List<MyUser?>>(context);
    for (var user in board) {
      print(user?.name);
      print(user?.points);

      _textList.add(
          Text("1. "   (user?.name ?? "")   toString(user?.points)   " p")); **Got the error under "user?points" **
    }

    print(_textList);

    return Container();
  }
}

user.dart:

class MyUser {
  final String? uid;
  final String? name;
  final int? points;
  String? groupId;

  MyUser({
    this.uid,
    this.name,
    this.points,
    this.groupId,
  });
}

CodePudding user response:

toString() doesn't accept any arguments, but you are passing it user?.points

Instead, you can try, user?.points.toString() to convert your points integer into a String.

Additionally, your Text widget could also look like this simply,

Text("1. ${user.name} ${user?.points}p");

CodePudding user response:

toString() returns a string representation of the object on which it is called. By using it like that, you are returning a representation of the instance of _LeaderBoard (it's as if you called this.toString()).

If you want to show the points in your Text widget, you should call:

user?.points.toString()

Instead of:

toString(user?.points)
  • Related