Home > Back-end >  Widget does not update as expected
Widget does not update as expected

Time:10-20

Stateless Scaffold does not update after updating 'parent'.

UserDetailEdit calls a function onSave(User) if the Userobject was changed. When I debug the call, the User is changed, so there is no issue with this Widget. 'us' is the changed user.

UserDetailLabelView is a Statlles-Scaffold-Widget.

My Debug results:

After onSave() and the call setState((){}) the UserDetailLabelViews method Widget build(BuildContext) is never called. Why is this the case? How can I update the Widget after 'popping' the edit one?


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

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

class _UserOverviewState extends State<UserOverview> {

  // ...
  User? detailUser;
  bool userEdit = false;
  // ...


  Widget _createUserView() {
      return UserDetailLabelView(
          user: this.detailUser!,
          onEdit: () async {
            this.userEdit = true;
            if (MediaQuery.of(context).size.width < 700) {
              await Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) =>  UserDetailEdit(
                user: this.detailUser!,
                onCancel: () => {
                  this.userEdit = false,
                  Navigator.of(context).pop(context),
                  print('Test')
                },
                    onSave: (us) {
                      this.detailUser = us;this.userEdit = false;
                      this.setState(() {});
                      Navigator.of(context).pop(context);

                    },
                  ))).then((value) => setState(() {}));
            }
            setState(() {
                print(this.detailUser!.roles.toString());
              });

          },
          editEnabled: this.checkEnabled(),
     )
  }
}

EDIT1: When I hit ctl s in the android studio and the app is refreshing, then the build method is triggered and View shows the correct changed user.

EDIT2: UserDetailLabelView

class UserDetailLabelView extends StatelessWidget {

  final User user;
  final Function onEdit;
  final editEnabled;

  const UserDetailLabelView({
    required this.user,
    required this.onEdit,
    this.editEnabled = true,
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (MediaQuery.of(context).size.width < 700) {
      return Scaffold(
        appBar: AppBar(
          title: Text('User Detail'),
          actions: <Widget>[
            IconButton(
              icon: Icon(
                Icons.edit,
                color: Colors.white,
              ),
              onPressed: () {
                if (this.editEnabled)
                  this.onEdit();
                print('Edit pressed');
              },
            )
          ],
        ),
        body: this.showDetail(this.user),
       );
     }
     return this.showDetail(this.user);
   }
}

CodePudding user response:

You cannot update a stateless widget like that. There is a similar question here: https://stackoverflow.com/a/66615499/15821771

  • Related