Home > Software design >  setState() or markNeedsBuild() called during build on dynamic widget creation
setState() or markNeedsBuild() called during build on dynamic widget creation

Time:09-21

I am facing an error setState() or markNeedsBuild() called during build while changing the state of the widget by calling changeState() function on onTap of InkWell element. Can anyone help me to find where I am doing wrong. I've tried all the possible solutions available over the internet. Following is my code:

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

  @override
  State<AdressenWidget> createState() => _AdressenWidgetState();
}

class _AdressenWidgetState extends State<AdressenWidget> {
  String _heading = '';
  String _name = '';
  String _address = '';
  String _telefon = '';
  String _email = '';
  bool _isChecked = false;

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

  Future initializeEAProvider() async {
    await locator<EAProvider>().initialize();
  }

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<EAProvider>(context, listen: true);
    provider.initialize();
    
    return Consumer<EAProvider>(
      builder: (context, v, child) => Container(
        margin: const EdgeInsets.fromLTRB(10, 5, 10, 5),
        padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
        decoration: auftragBox,
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Visibility(
            visible: v.expandAdrWidget,
            child: SizedBox(
              child: mainBody(v.addressList, v.isDashboard),
            ),
          )
        ]),
      ),
    );
  }

  Widget mainBody(List<Addressobject> addressList, bool isExpanded) {
    var adress = getData(addressList, list[0]);
    _heading = adress.heading;
    _name = adress.name;
    _address = adress.address;
    _telefon = adress.telefon;
    _email = adress.email;
    _isChecked = adress.isChecked;

    return Column(
      children: [
      IntrinsicHeight(
                child: Row(
                children: [
                  SizedBox(
                    height: 170,
                    child: Column(
                      children: [
                        for (var i = 0; i < 6; i  )
                          InkWell(
                            onTap: () {
                              changeState(addressList, i); //here is the issue
                            },
                            child: const Icon(
                              Icons.location_pin,
                              color: Colors.black,
                              size: 20.0,
                            ),
                          ),
                      ],
                    ),
                  ),
                ],
              ))],
    );
  }

  

  void changeState(List<Addressobject> addressList, int index) {
    var adress = getData(addressList, list[index]);
    setState(() {
      _heading = adress.heading;
      _name = adress.name;
      _address = adress.address;
      _telefon = adress.telefon;
      _email = adress.email;
      _isChecked = adress.isChecked;
    });
  }

  initialize() async {
    addressList = address_model.fromJson(addressjson).addressobject!;
    notifyListeners();
  }

CodePudding user response:

you cant call 'setState' or changeNotifier before build. because the widget doesnt mounted yet.

maybe you can try this

 Future initializeEAProvider() async {
    await Future.delayed(const Duration());
    await locator<EAProvider>().initialize();
  }

its not good maybe ,but you can try it

CodePudding user response:

Wrap your provider.initialize() with widgetsbinding

WidgetsBinding.instance!.addPostFrameCallback((_) => provider.initialize(),);
  • Related