skip to Main Content

The following RangeError was thrown building:
RangeError (index): Invalid value: Only valid value is 0: 1

how can I rectify this error, Can you please tell me what’s the issue with my code and how to avoid showing that red screen error in my app?

It displays a red enter image description herescreen

  List<String> attachments = [];
 
  List<String> imageName = [];

  bool isHTML = false;
  final _formKey = GlobalKey<FormState>();
 

   

  @override
  Widget build(BuildContext context) {
    return Scaffold(
       
        ),
        builder: (context) => Form(
              key: _formKey,
              child: ListView(
                children: [
                  SizedBox(height: kRegularPadding),
                  
                 
                  Column(
                    children: <Widget>[
                      InkWell(
                        onTap: _addImage,
                        child: SizedBox(
                            width: 343.h,
                            height: 165.h,
                            child: Image.asset(
                              AssetImagePaths.uploadImage,
                              fit: BoxFit.contain,
                            )),
                      ),
                      SizedBox(
                        height: kLargePadding,
                      ),
                      ListView.builder(
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        itemCount: attachments.length,
                        itemBuilder: (context, index) {
                          return Padding(
                            padding:
                                EdgeInsets.symmetric(vertical: kRegularPadding),
                            child: Container(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 15),
                              height: 75.h,
                              width: 343.h,
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  border: Border.all(color: gray7Color)),
                              child: Row(
                                children: [
                                  Container(
                                    height: 43,
                                    width: 43,
                                    decoration: const BoxDecoration(
                                      color: navy5Color,
                                      shape: BoxShape.circle,
                                    ),
                                    child: const Icon(
                                      Icons.insert_drive_file,
                                      color: navy1Color,
                                    ),
                                  ),
                                  SizedBox(
                                    width: kSmallPadding,
                                  ),
                                  Expanded(
                                    child: Text(
                                      imageName[index],
                                      style: Theme.of(context)
                                          .textTheme
                                          .caption!
                                          .copyWith(
                                              color: gray3Color,
                                              fontWeight: FontWeight.w400),
                                    ),
                                  ),
                                  const Spacer(),
                                  Text(
                                    "100%",
                                    style: Theme.of(context)
                                        .textTheme
                                        .caption!
                                        .copyWith(
                                            color: gray3Color,
                                            fontWeight: FontWeight.w400),
                                  ),
                                  SizedBox(
                                    width: kSmallPadding,
                                  ),
                                  IconButton(
                                    onPressed: () => {_removeAttachment(index)},
                                    icon: const Icon(
                                      Icons.cancel,
                                      color: gray3Color,
                                      size: 24,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          );
                        },
                      )

                 
                      ,
                      SizedBox(
                        height: kRegularPadding,
                      ),
                    ],
                  ),
                  SizedBox(height: kMediumPadding),
                ],
              ),
            ));
  }

  void _removeAttachment(int index) {
    setState(() {
      attachments.removeAt(index);
    });
  }

 

  Future<void> _addImage() async {
    final ImagePicker picker = ImagePicker();
    final XFile? image = await picker.pickImage(source: ImageSource.gallery);
    print(image?.path);
    if (image != null) {
      setState(() {
        String _imagePath = image!.path;
        attachments.add(_imagePath);
        imageName = [_imagePath.split('/').last];
        print(_imagePath.split('/').last);
      });
    }
  }
 

Being able to add more than 5 images.

2

Answers


  1. Chosen as BEST ANSWER

    This was the answer to it

     Future<void> _addImage() async {
        final ImagePicker picker = ImagePicker();
        final XFile? image = await picker.pickImage(source: ImageSource.gallery);
        print(image?.path);
        if (image != null) {
          setState(() {
            // String _imagePath = image.path;
            attachments.add(image.path.split('/').last);
    
            // imageName = [_imagePath.split('/').last];
            // print(_imagePath.split('/').last);
          });
        }
      }
    

  2. I think you just made mistake here.

    Future<void> _addImage() async {
        final ImagePicker picker = ImagePicker();
        final XFile? image = await picker.pickImage(source: ImageSource.gallery);
        print(image?.path);
        if (image != null) {
          setState(() {
            String _imagePath = image!.path;
            attachments.add(_imagePath);
            imageName.add(_imagePath.split('/').last);  <------- just need to add name in the list
            print(_imagePath.split('/').last);
          });
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search