0

I want to pass the instance of class Paricipant to the tab bars. I tried to make the List late, but it didn't show the information at the begining(I have to go to another page and go back to see).

class MainTabBar extends StatelessWidget {
  MainTabBar({Key? key, required this.userInfo}) : super(key: key);

  final ParticipantInfo userInfo;

  final List<ItemView> items = <ItemView>[
    ItemView(title: "User", icon: Icons.account_box, view: UserProfileTab(userInfo: userInfo)),
    ItemView(title: "Current State", icon: Icons.content_paste, view: CurrentStatesTab(roomNumber: userInfo.location,)),
    const ItemView(title: "All data", icon: Icons.table_rows_rounded, view: AllInfoTab()),

  ];

  @override
  Widget build(BuildContext context) {......
lib/main.dart:44:85: Error: Can't access 'this' in a field initializer to read 'userInfo'.
    ItemView(title: "User", icon: Icons.account_box, view: UserProfileTab(userInfo: userInfo)),
                                                                                    ^^^^^^^^
lib/main.dart:45:100: Error: Can't access 'this' in a field initializer to read 'userInfo'.
    ItemView(title: "Current State", icon: Icons.content_paste, view: CurrentStatesTab(roomNumber: userInfo.location,)),

1 Answers1

0

You cannot use an instance variable to initialize another instance variable like that.

class Product {
  final String name;
  final int price = name.length
  
  Product({required this.name});
}

Instead, initialize it within the constructor:

class Product {
  final String name;
  final int price;
  
  Product({required this.name}): price = name.length;
}
Thierry
  • 7,775
  • 2
  • 15
  • 33