0

I have a flutter application and I keep getting the error which states:

The following assertion was thrown while dispatching notifications for UserData:
setState() or markNeedsBuild() called during build.

UserData is a class that extends ChangeNotifier and this exception is thrown when I call the notifyListeners() method.

My View

class MyViewextends StatefulWidget {
  @override
  _MyViewState createState() => _MyViewState();
}

class _MyViewState extends State<MyView> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: UserService().getUser(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done)
            {
              var userData = Provider.of<UserData>(context, listen: false);
              var user = (snapshot.data as ApiResponseModel)?.entity;
              userData.set(user); //this is where the error gets thrown
            }

          return Container(); 
          //shortened for question purposes. Ideally, I will be printing the User objects to the container. 
          //It containers String properties like firstName, lastName.
        });
  }
}

UserData

class UserData extends ChangeNotifier{
  UserDto user;

  void set(UserDto userDto){
    user = userDto;
    notifyListeners();
  }

What I want to know is, what better way can I initialise my UserDto object so this error is not thrown?

In my head, that if block made the most sense but apparently not.

steve
  • 797
  • 1
  • 9
  • 27
  • 1
    What is above `MyView` in the widget tree? Is it possible that your `ChangeNotifier` is listened by some widget above? Because in this case `userData.set(user)` can cause a rebuild. You can try: `WidgetsBinding.instance!.addPostFrameCallback((timeStamp) { userData.set(user); });` instead of simply calling. – Peter Koltai Apr 10 '22 at 20:43
  • @PeterKoltai thanks it worked! Can you explain to me what this does please? – steve Apr 11 '22 at 21:42
  • 1
    The error you got means that the widget is marked to be rebuilt during another build is in progress. The `addPostFrameCallback` executes the passed callback after the current build is completed, therefore eliminates to problem of duplicate builds at the same time. – Peter Koltai Apr 12 '22 at 05:26

0 Answers0