Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving setState() or markNeedsBuild() called during build. #5

Closed
wants to merge 2 commits into from

Conversation

lulupointu
Copy link

This issue appeared when a user trying to call setState in the onChanged function.

This appears because of the call to _determine which caused a call to setState during initState.

You can try the following code to raise the error and see that my code fixes it (This is just you main example with a Container changing color):

import 'package:flutter/material.dart';
import 'package:lite_rolling_switch/lite_rolling_switch.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(
        title: 'Examples',
        home: MyHomePage(),
      );
}

class MyHomePage extends StatefulWidget {
  MyHomePage({
    Key key,
  }) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  bool changeColor = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              height: 100,
              color: changeColor ? Colors.green : Colors.red,
            ),
            //By default
            LiteRollingSwitch(
              value: false,
              onChanged: (bool state) {
                print('turned ${(state) ? 'on' : 'off'}');
              },
            ),

            //Customized
            Padding(
              padding: EdgeInsets.only(top: 20),
              child: LiteRollingSwitch(
                value: true,
                textOn: 'active',
                textOff: 'inactive',
                colorOn: Colors.deepOrange,
                colorOff: Colors.blueGrey,
                iconOn: Icons.lightbulb_outline,
                iconOff: Icons.power_settings_new,
                onChanged: (bool state) {
                  print('turned ${(state) ? 'on' : 'off'}');
                  print('CHANGE COLOR');
                  setState(() {
                    changeColor = !changeColor;
                  });
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

This issue appeared when a user trying to call setState in the onChanged function.

This appears because of the call to _determine which caused a call to setState during initState.
@rodrigobastosv
Copy link
Contributor

I guess i solved this more elegantly on #6 .

Take a look please

@lulupointu
Copy link
Author

Yes that nice 👍

@lulupointu lulupointu closed this Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants