我得到了一个“1位置参数(s)预期,但0发现,”错误在Flutter使用 dart 与云Firestore?

hrirmatl  于 2023-09-28  发布在  Flutter
关注(0)|答案(1)|浏览(78)
return ListView.builder(
  itemCount: users.length,
  itemBuilder: (context, index){
    return UserTile(sport: users[index]);
  },
);

我得到了一个“1位置参数(s)预期,但0发现.”错误,我不知道如何修复它.这是我的UserTile文件

class UserTile extends StatelessWidget {
     final Sport sport;
     UserTile(this.sport, {users});

@override
Widget build(BuildContext context) {
     return Padding(
         padding: const EdgeInsets.only(top: 8.0),
         child: Card(
             margin: EdgeInsets.fromLTRB(20.0, 6.0, 20.0, 0.0),
             child: ListTile(
                leading: CircleAvatar(
                   radius: 25.0,
                   backgroundColor: Colors.brown[100],
                ),
                title: Text(sport.name),
                subtitle: Text('User plays ${sport.sport}'),
             ),
         ),
    );
}

}

e4eetjau

e4eetjau1#

final Sport sport;
final Users users;
UserTile(this.sport, {users});

这意味着你可以这样做:

Sport sport = Sport();
UserTile(sport);
UserTile(sport,users:someUsers);

params out of {}[],表示必须,与其他语言相同。

class UserTile{
  final Sport sport;
  UserTile(this.sport);
}
UserTile userTile = UserTile(sport);

{}中的params,表示需要命名。

class UserTile{
  final Sport? sport; //? means can be now.
final Sport requiredSport;// or required:
  UserTile({this.sport,required this.requiredSport});
}
UserTile userTile = UserTile(sport:sport,requiredsport:sport);

[]中的params,表示它是可选的。

class UserTile{
  final Sport? sport; //? means can be now.
final Sport requiredSport;// or required:
  UserTile(this.requiredSport,[this.sport]);
}
UserTile userTile = UserTile(requiredSport,sport);

相关问题