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,
});
}
2
Answers
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:
Instead of:
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,