My Flutter App is Not Working on Android 11 But Working fine in emulator as well as android 8 || (OS Error: Operation not permitted, errno = 1)

Issue

Here is My Code

import 'package:flutter/material.dart';
import 'package:screenshot/screenshot.dart';
import 'package:sweld/globals.dart';
import 'package:geolocator/geolocator.dart';

class WeldSetup extends StatelessWidget {
  WeldSetup({Key? key}) : super(key: key);
  final pageNum = 211;

  @override
  Widget build(BuildContext context) {
    final _ssc = ScreenshotController();
    return Scaffold(
      appBar: AppBar(
        title: Text("Weld Setup"),
        centerTitle: true,
        actions: [
          IconButton(
            onPressed: () {
              Navigator.pop(context);
            },
            icon: Icon(Icons.railway_alert_rounded),
          ),
        ],
      ),
      body: Screenshot(controller: _ssc, child: WsLocation()),
      persistentFooterButtons: [
        ElevatedButton(
            onPressed: () async {
              Globals.ss211 = await _ssc.captureAndSave(Globals.appDir,
                  fileName: "ss211.png",
                  pixelRatio: MediaQuery.of(context).devicePixelRatio,
                  delay: Duration(milliseconds: 100));

              Navigator.pushNamed(context, "/212");
            },
            child: Text("Next"))
      ],
      extendBody: true,
    );
  }
}

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

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

class _WsLocationState extends State<WsLocation> {
  late Position curPos;
  var longitute = Globals.longitude;
  var latitude = Globals.latitude;
  TimeOfDay sTime = TimeOfDay(hour: 00, minute: 00);
  TimeOfDay eTime = TimeOfDay(hour: 00, minute: 00);

  Widget selectTime(time) {
    if (time == TimeOfDay(hour: 00, minute: 00)) {
      return Text("Select Time");
    } else {
      if (time == sTime) {
        return Text("${Globals.stime}");
      } else {
        return Text("${Globals.etime}");
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        margin: EdgeInsets.all(15),
        color: Colors.white,
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "LOCATION",
                  textScaleFactor: 2,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                  ),
                )
              ],
            ),
            Divider(
              thickness: 2,
              height: 50,
            ),
            Container(
              child: Form(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Flexible(
                          child: TextFormField(
                            initialValue: Globals.longitude,
                            decoration: InputDecoration(
                              labelText: "Longitude",
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(5),
                                borderSide: BorderSide(
                                  color: Colors.black,
                                  style: BorderStyle.solid,
                                  width: 1.5,
                                ),
                              ),
                            ),
                            onChanged: (val) {
                              setState(() {
                                Globals.longitude = val;
                              });
                            },
                          ),
                        ),
                        Divider(
                          indent: 5,
                        ),
                        Flexible(
                          child: TextFormField(
                            initialValue: Globals.latitude,
                            decoration: InputDecoration(
                              labelText: "Latitude",
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(5),
                                borderSide: BorderSide(
                                  color: Colors.black,
                                  style: BorderStyle.solid,
                                  width: 1.5,
                                ),
                              ),
                            ),
                            onChanged: (val) {
                              setState(() {
                                Globals.latitude = val;
                              });
                            },
                          ),
                        ),
                      ],
                    ),
                    Divider(
                      thickness: 0,
                      height: 10,
                    ),
                    TextFormField(
                      initialValue: Globals.section,
                      decoration: InputDecoration(
                        labelText: "Section",
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(5),
                          borderSide: BorderSide(
                            color: Colors.black,
                            style: BorderStyle.solid,
                            width: 1.5,
                          ),
                        ),
                      ),
                      onChanged: (val) {
                        setState(() {
                          Globals.section = val;
                        });
                      },
                    ),
                    Divider(
                      thickness: 0,
                      height: 10,
                    ),
                    TextFormField(
                      initialValue: Globals.division,
                      decoration: InputDecoration(
                        labelText: "Division",
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(5),
                          borderSide: BorderSide(
                            color: Colors.black,
                            style: BorderStyle.solid,
                            width: 1.5,
                          ),
                        ),
                      ),
                      onChanged: (val) {
                        Globals.division = val;
                      },
                    ),
                    Divider(
                      thickness: 0,
                      height: 10,
                    ),
                    TextFormField(
                      initialValue: Globals.distance,
                      decoration: InputDecoration(
                        labelText: "Distance",
                        hintText: "In kilometers(km)",
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(5),
                          borderSide: BorderSide(
                            color: Colors.black,
                            style: BorderStyle.solid,
                            width: 1.5,
                          ),
                        ),
                      ),
                      onChanged: (val) {
                        Globals.distance = val;
                      },
                    ),
                    Divider(
                      thickness: 0,
                      height: 10,
                    ),
                    DropdownButtonFormField<String>(
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(5),
                          borderSide: BorderSide(
                            color: Colors.black,
                            style: BorderStyle.solid,
                            width: 1.5,
                          ),
                        ),
                      ),
                      hint: Text("Line"),
                      isExpanded: true,
                      items: <String>['Single', 'Up-Line', 'Down-Line']
                          .map((String value) {
                        return DropdownMenuItem<String>(
                          value: value,
                          child: Text(value),
                        );
                      }).toList(),
                      onChanged: (val) {
                        Globals.line = val;
                      },
                    ),
                    Divider(
                      thickness: 0,
                      height: 10,
                    ),
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Flexible(
                            child: Row(
                              children: [
                                Divider(
                                  indent: 15,
                                  endIndent: 5,
                                ),
                                Text("Start Time "),
                                TextButton(
                                  onPressed: () {
                                    showTimePicker(
                                            context: context,
                                            initialTime: sTime)
                                        .then((value) {
                                      if (value != null) {
                                        setState(() {
                                          sTime = value;
                                          Globals.stime = value.format(context);
                                        });
                                      }
                                    });
                                  },
                                  child: Globals.stime == null
                                      ? selectTime(sTime)
                                      : Text(" ${Globals.stime} "),
                                ),
                              ],
                            ),
                          ),
                          // Divider(
                          //   thickness: 0,
                          //   indent: 5,
                          // ),
                          Flexible(
                            child: Row(
                              children: [
                                Divider(
                                  indent: 10,
                                ),
                                Text("End Time "),
                                TextButton(
                                  onPressed: () {
                                    showTimePicker(
                                            context: context,
                                            initialTime: eTime)
                                        .then((value) {
                                      if (value != null) {
                                        setState(() {
                                          eTime = value;
                                          Globals.etime = value.format(context);
                                        });
                                      }
                                    });
                                  },
                                  child: Globals.etime == null
                                      ? selectTime(eTime)
                                      : Text(" ${Globals.etime} "),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Divider(
              height: 50,
            ),
          ],
        ),
      ),
    );
  }
}

Here user is able to Input in Text fields, But when He presses Next He is not able to goto next Page. Moreover When I run it in emulator then it is working Totally Fine !.
And Yes its important to take screenshot of everypage therefore, I cannot Exclude it.
And sometime it returns Null, It Might be a problem But I dont know How to deal with it or What should I do Next.
Any Suggestions Please

Here is My Build Gradle File

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 30

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.sweld"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

UpDate

I just Recieved the error, It is

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Creation failed, path = 'storage/emulated/0/sweld' (OS Error: Operation not permitted, errno = 1)
E/flutter ( 7337): #0      _Directory.create.<anonymous closure> (dart:io/directory_impl.dart:117:11)
E/flutter ( 7337): #1      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter ( 7337): #2      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter ( 7337): <asynchronous suspension>

So Please Tell me what SHow I DO ??

Solution

I solved this issue My getting Some additional Permissions.

MANAGE_EXTERNAL_STORAGE and INTERNET Permission

in Android Manifest File

Answered By – kabir

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *