Home > Net >  flutter crash when i push Button upload image
flutter crash when i push Button upload image

Time:11-11

I create the upload page after i Finnish the code that follow youtube I try to upload image in my app but when i pushed my button the app goes crash and Lost connection to device, so i go back to check my code and nothing show error. In terminal doesn't show any information i don't know how to resolve my code.

here is my code

    class PostView extends StatefulWidget {
      @override
      State<PostView> createState() => _PostViewState();
    }
    
    class _PostViewState extends State<PostView> {
      
      List<File> _image = [];
      final ImagePicker picker = ImagePicker();
    
      Future chooseImage() async {
        final pickedFile = await picker.pickImage(source: ImageSource.gallery);
        setState(() {
          _image.add(File(pickedFile!.path));
        });
        if (pickedFile?.path == null) retrieveLostData();
      }
    
      Future<void> retrieveLostData() async {
        final LostData response = await picker.getLostData();
        if (response.isEmpty) {
          return;
        }
        if (response.file != null) {
          setState(() {
            _image.add(File(response.file!.path));
          });
        } else {
          print(response.file);
        }
      }

      @override
      Widget build(BuildContext context) {
        return Sizer(builder: (context, orientation, deviceType) {
          return Scaffold(
              body: SafeArea(
            left: false,
            right: false,
            minimum: const EdgeInsets.all(0),
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  const SizedBox(width: 10, height: 0),
                  Container(
                    child: Column(children: [
                      const SizedBox(width: 10, height: 10),
                      CarouselSlider(
                        items: [
                          Container(
                            margin: EdgeInsets.all(2),
                            child: StaggeredGridView.countBuilder(
                                shrinkWrap: true,
                                crossAxisCount: 2,
                                crossAxisSpacing: 12,
                                mainAxisSpacing: 12,
                                itemCount: _image.length   1,
                                itemBuilder: (context, index) {
                                  return index == 0
                                      ? SizedBox(
                                          width: double.infinity,
                                          height: 80.0,
                                          child: OutlinedButton(
                                            child: Text(
                                              'Upload',
                                              style: TextStyle(
                                                color: HexColor("7225FF"),
                                              ),
                                            ),
                                            onPressed: () {
                                              chooseImage();
                                            },
                                            style: ElevatedButton.styleFrom(
                                              side: BorderSide(
                                                width: 1.5,
                                                color: HexColor("7225FF"),
                                              ),
                                              shape: RoundedRectangleBorder(
                                                borderRadius:
                                                    BorderRadius.circular(14.0),
                                              ),
                                            ),
                                          ))
                                      : Container(
                                          margin: EdgeInsets.all(3),
                                          decoration: BoxDecoration(
                                              image: DecorationImage(
                                                  image:
                                                      FileImage(_image[index - 1]),
                                                  fit: BoxFit.cover)),
                                        );
                                  
                                },
                                staggeredTileBuilder: (index) {
                                  return new StaggeredTile.count(
                                      1, index.isEven ? 1.2 : 1.8);
                                }),
                            
                          ),
                        ],
                        options:
                            CarouselOptions(aspectRatio: 1, viewportFraction: 1),
                      ),
                    ]),
                  ),
                ],
              ),
         ),
            ),
          ));
        });
      }
    }
    
    

this is my podFile info

# Uncomment this line to define a global platform for your project
platform :ios, '13.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)

    target.build_configurations.each do |config|
      # You can remove unused permissions here
      # for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
      # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',

        'PERMISSION_MEDIA_LIBRARY=1',

      ]

    end
  end
end

info.plst

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>alphanew</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(FLUTTER_BUILD_NAME)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string></string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string></string>
            </array>
        </dict>
        <dict>
            <key>CFBundleURLType</key>
            <array>
                <dict>
                    <key>CFBundleTypeRole</key>
                    <string>Editor</string>
                    <key>CFBundleURLSchemes</key>
                    <array>
                        <string>My firebase Reverse url</string>
                    </array>
                </dict>
            </array>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>My firebase Reverse url</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>$(FLUTTER_BUILD_NUMBER)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
</dict>
</plist>

CodePudding user response:

Add permission in PodFile like this

'PERMISSION_CAMERA=1',

For reference. https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/example/ios/Podfile

  • Related