'What is the best way to use ChangeNotifier provider objects with the build method?
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
