Home > database >  Future,setState ,error: This expression has a type of 'void' so its value can't be us
Future,setState ,error: This expression has a type of 'void' so its value can't be us

Time:02-28

Container(
                  margin: EdgeInsets.fromLTRB(220.w,155.h,0,0),
                  child: IconButton(
                    onPressed: (){
                       ChangeProfile(context).then(
                          setState(() {
                            profile = pro.currentProfile;
                          })
                      );
                    },
                    icon: Icon(Icons.wifi_protected_setup),
                  ),
                ),

This is the part that executes the ChangeProfile function. Change the variable called profile that is output to the main after it is executed

PrintImage(
                  imagetext: profile,
                  xsize: 100.0,
                  ysize: 100.0,
                  hhh: 90.0,
                ),

output like this

Future<void> ChangeProfile(BuildContext context) {
  final pro = Provider.of<Pro>(context, listen: false);
  US(context);
  int count = 0;
  return showDialog<void>(
    context: context,
    builder: (context) {
      return StatefulBuilder(

        builder: (BuildContext context, StateSetter setState) {
          return AlertDialog(
            // RoundedRectangleBorder - Dialog 화면 모서리 둥글게 조절
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0)),
            //Dialog Main Title
            title: Column(
              children: <Widget>[
                Container(
                  width: 50.w,
                  height: 50.h,
                  child: Image.asset(
                    pro.currentProfile
                  ),
                )
              ],
            ),
            //
            content: SizedBox(
              width: 230.w,
              height: 230.h,
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SizedBox(

                        width: 70.w,
                        height: 70.h,
                        child: OutlinedButton(
                          onPressed: (){
                            setState(() { // setState() 추가.
                              pro.currentProfile= 'image_profile/profile_1.png';
                            });
                          },
                          child: Image.asset('image_profile/profile_1.png'),
                        ),
                      ),
                      SizedBox(
                        width: 70.w,
                        height: 70.h,
                        child: OutlinedButton(
                          onPressed: (){
                            setState(() { // setState() 추가.
                              pro.currentProfile= 'image_profile/profile_2.png';
                            });
                          },
                          child: Image.asset('image_profile/profile_2.png'),
                        ),
                      ),

Someone told me to use a Future so that the AlertDialog can be finished and the screen refreshed, but I get an error

error: This expression has a type of 'void' so its value can't be used. (use_of_void_result at [goodman] lib\3_userscreen\userscreen.dart:74)

In the AlertDialog, I checked the real-time change by using the Provider to display the image with StatefulBuilder. The part output from the parent widget contains the contents of the Provider, but it is not reflected and has to be applied by pressing another button.

I used FUTURE and then, but I get a void error

This is the code before feedback : Statefulwidget does not refresh after AlertDialog is closed

CodePudding user response:

This part is not correct:

onPressed: (){
  ChangeProfile(context).then(
    setState(() {
      profile = pro.currentProfile;
    })
  );
},

.then requires you to pass it a callback function like this:

onPressed: (){
  ChangeProfile(context).then((_) {
    setState(() {
      profile = pro.currentProfile;
    });
  });
},

Alternatively, you could use async/await:

onPressed: () async {
  await ChangeProfile(context);
  setState(() {
    profile = pro.currentProfile;
  });
},
  • Related