-1

I wrote a code that send a map between two widgets and i get this error when I send the map from the first code to the other. Someone know what is my problem and how can I solve it?.

this is the relevant part of the first code:

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

  @override
  _HomeBState createState() => _HomeBState();
}

class _HomeBState extends State<HomeB> {

  Map<String, dynamic> barberData = {};

  @override
  void initState() {
    _getDataAndPrint();
    super.initState();
  }

  Future<void> _getDataAndPrint() async {
    barberData = await getBarberData();
  }

  final tabs = [
    widget1(),
    widget2(),
    widget3,
    ProfileB(barberData: barberData,) // here I get this error
  ];

and this is the code that suppose to get the map:

class ProfileB extends StatefulWidget {
  Map<String, dynamic> barberData;
  ProfileB({Key? key, required this.barberData}) : super(key: key);
  @override
  _ProfileBState createState() => _ProfileBState();
}

class _ProfileBState extends State<ProfileB> {

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;

    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: ConstrainedBox(
            constraints: BoxConstraints(
              minHeight: size.height - 90,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Padding(
                  padding: EdgeInsets.only(
                    top: size.height * .005,
                  ),
                  child: Center(
                    child: Text(
                      widget.barberData["field1"],
                      style: TextStyle(
                        fontSize: size.width * .037,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}

The erros I get:

"Error: Can't access 'this' in a field initializer to read 'barberData'. ProfileB(barberData: barberData,)"

"The instance member 'barberData' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression"

roee attias
  • 153
  • 4
  • 15

1 Answers1

1

You cannot add a future variable in the initializer. The example below will solve your problem.

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

  @override
  _HomeBState createState() => _HomeBState();
}

class _HomeBState extends State<HomeB> {

  Map<String, dynamic> barberData = {};
  final tabs = [
    widget1(),
    widget2(),
    widget3,
  ];

  @override
  void initState() {
    _getDataAndPrint();
    super.initState();
  }

  Future<void> _getDataAndPrint() async {
    barberData = await getBarberData();
tabs.add(ProfileB(barberData: barberData,);  
}
Salih Can
  • 1,562
  • 1
  • 9
  • 18