FileSystemException while saving an image to gallery in Android Studio Emulator using Flutter

I’m developing a flutter application for the first time. I want to be able to paint over an image and save that image to the phone’s gallery, but I’m having some trouble because of permission handling. It says Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/FileName.png' (OS Error: Operation not permitted, errno = 1)
The relevant code is mentioned below:

//In _doodleCreateState //which is a stateful widget
Future<ui.Image> get rendered {                         
  ui.PictureRecorder recorder = ui.PictureRecorder();   
  Canvas canvas = Canvas(recorder);                     
  MyPainter painter = MyPainter(pointsList: points);    
  var size = context.size;                              
  painter.paint(canvas, size);                          
  return recorder                                       
      .endRecording()                                   
      .toImage(size.width.floor(), size.height.floor());
} 
//In build function of _DoodleCreateState
new IconButton(                           
    icon: Icon(Icons.save_alt_outlined),  
    onPressed: () {                       
      setState(() {                       
        Navigator.push(                   
          context,                        
          new MaterialPageRoute(          
            builder: (_) => new SignApp(),
           )
        ); 
      })
    })

 
class SignApp extends StatefulWidget {                  
  @override                                             
  State<StatefulWidget> createState() {                 
    return SignAppState();                              
  }                                                     
}                                                       
                                                        
class SignAppState extends State<SignApp> {             
  GlobalKey<_DoodleCreateState> doodleKey = GlobalKey();
  Permission _permission = Permission.WriteExternalStorage;
  var images;   

//inside build function of SignAppState
FlatButton(                     
  child: Text('Save'),          
  onPressed: () {               
    setRenderedImage(context);  
  },                            
)

setRenderedImage(BuildContext context) async {                    
  print("Inside setRenderedImage function");                      
  ui.Image renderedImage = await doodleKey.currentState.rendered; 
                                                                  
  setState(() {                                                   
    images = renderedImage;                                       
  });                                                             
                                                                  
  showImage(context);                                             
}

Future<Null> showImage(BuildContext context) async {                            
  print("Inside showImage Function");                                           
  var pngBytes = await images.toByteData(format: ui.ImageByteFormat.png);
       
  if (!(await checkPermission())){                                              
    await requestPermission();                                                  
  }                                                                             
  
  //For getting the current directory path                                                                                                   
  Directory directory = await getExternalStorageDirectory();                    
  String path = directory.path;                                                 
  print(path);                                                                  
  
  //For creating a new path where android directory is located on phone                                                                 
  String newPath = "";                                                          
  List<String> folders = directory.path.split("/");                             
  for(int i=1; i<folders.length; i++){                                          
    String folder = folders[i];                                                 
    if (folder != "Android"){                                                   
      newPath = newPath + "/" + folder;                                         
    }else{                                                                      
      break;                                                                    
    }                                                                           
  }
                                                                             
  newPath = newPath + "/Swadeshi Doodle";                                       
  directory = Directory(newPath);                                               
  print(directory.path);                                                        
                                                                                
  File(newPath + "image.png").writeAsBytesSync(pngBytes.buffer.asInt8List());   
  return showDialog<Null>(                                                      
      context: context,                                                         
      builder: (BuildContext context) {                                         
        return AlertDialog(                                                     
          title: Text(                                                          
            'Please check your device\'s Swadeshi Doodle folder',               
            style: TextStyle(                                                   
                fontFamily: 'Landing',                                          
                fontWeight: FontWeight.w300,                                    
                color: Theme.of(context).primaryColor,                          
                letterSpacing: 1.1),                                             
          ),                                                                     
          content: Image.memory(Uint8List.view(pngBytes.buffer)),               
        );                                                                       
      });                                                                       
}   

requestPermission() async {
    final result = (await SimplePermissions.requestPermission(_permission));
    return result;
  }

  checkPermission() async {
    bool result = await SimplePermissions.checkPermission(_permission);
    return result;
  }

  getPermissionStatus() async {
    final result = await SimplePermissions.getPermissionStatus(_permission);
    print("permission status is " + result.toString());
  }

The output prints that the control went inside both setRenderedImage function and showImage function. It checks the permission and prints I/SimplePermission( 2298): Checking permission : android.permission.WRITE_EXTERNAL_STORAGE. Then it also prints the current folder and the modified path of directory which I want to create. But as soon as i click save, it gives the filesystemexception.

My android manifest file looks like this:

<manifest ...>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

   <application
        android:label="sdoodle"
        android:requestLegacyExternalStorage="true"

                            .....
                            .....
   </application>
</manifest>

In the pubspec.yaml file, I have added image_gallery_saver: ^1.5.0, shared_preferences: ^0.5.7, path_provider: ^0.4.1, and

simple_permissions:
    git:
      url: https://github.com/tq-systems/simple_permissions.git

I have searched a lot but I can’t find a solution that works for me. I followed this answer too https://stackoverflow.com/a/67151803/11765864 , and changed the MainActivity.kt file but it didn’t help my case

Please tell me what am I doing wrong.

I will need to search more about it, but have you already tried on a real device? Facing same issue there as well?