Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I built an image picker after learning in a course. I am not getting any errors in the debug console or in the build. I am trying to click it but it is not working. I made another image picker in a similar manner in same app but that did not gave any error

My add image screen:

class _AddImageState extends State<AddImage> {
  bool uploading = false;
  double val = 0;
  CollectionReference? imgRef;
  firebase_storage.Reference? ref; 

  List<File> _image = []; 
  final picker = ImagePicker();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Add Image'),
        actions: [
          ElevatedButton(
            onPressed: () {
              setState(() {
                uploading = true;
              });
              
              uploadFile().whenComplete(() => Navigator.of(context).pop());
            },
            child: Text(
              'Upload',
              style: TextStyle(color: Colors.red),
            ),
          )
        ],
      ),
      body: Stack(children: [
        GridView.builder(
            itemCount: _image.length + 1,
            gridDelegate:
                SliverGridDelegateWithMaxCrossAxisExtent(maxCrossAxisExtent: 1),
            itemBuilder: (ctx, i) {
              return i == 0
                  ? Center(
                      child: IconButton(
                          onPressed: () {
                            !uploading ? chooseImage() : null;
                          },
                          icon: Icon(Icons.add)),
                    )
                  : Container(
                      margin: EdgeInsets.all(3),
                      decoration: BoxDecoration(
                          image:
                              DecorationImage(image: FileImage(_image[i - 1]))),
                    );
            }),
        uploading
            ? Center(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                        child: Text('uploading...',
                            style: TextStyle(fontSize: 20))),
                    CircularProgressIndicator.adaptive(
                      
                      value: val,
                      valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
                    )
                  ],
                ),
              )
            : Container()
      ]),
    );
  }


choose image function :

chooseImage() async {
    
    final pickedFile = await picker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image.add(File(pickedFile!.path));
    });
    if (pickedFile!.path == null) retrieveLostData();
  }


What I have tried:

I tried onPressed: chooseImage; but it still did not work,
Posted
Comments
wseng 30-Jan-22 7:16am    
perhaps chooseImage method not getting called?
Kavya Bhargava 30-Jan-22 8:58am    
Could you show how can i make that change,would be helpful
wseng 30-Jan-22 12:24pm    
maybe add some print function in chooseImage, check whether the method is called.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900