How to upload image in Flutter using Image Picker

1,735

there is no onSaved property in the Center widget. You can use a button instead and utilize the onPressed property or any other approach that you prefer.

Share:
1,735
Avrilla Wardani
Author by

Avrilla Wardani

Updated on December 13, 2022

Comments

  • Avrilla Wardani
    Avrilla Wardani over 1 year

    I tried to save image in image upload form using image picker, I used "OnSaved" but there are error said "The named Parameter OnSaved isnt defined. How can I solve this?

    This is the code that i use :

    onSaved: (val) => _image = val,
    

    This is the complete code:

    import 'dart:io';
    import 'package:image_picker/image_picker.dart';
    
    class NewPostScreen extends StatefulWidget {
      @override
    _NewPostScreen createState() => _NewPostScreen();
    }
    
    class _NewPostScreen extends State<NewPostScreen> {
      File _image;
    
      Future getImageFromGallery() async {
        var image = await ImagePicker.pickImage(source: ImageSource.gallery);
        setState(() {
          _image = image;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return Container(
          child: Scaffold(
            key: _scaffoldkey,
            appBar: AppBar(
              backgroundColor: Colors.blue,
              title: new Text("Create Post"),
            ),
            resizeToAvoidBottomPadding: false,
            body: SingleChildScrollView(
              padding: new EdgeInsets.all(8.0),
              child: Form(
                key: _formkey,
                child: Column(
                  children: <Widget>[
                    new TextFormField(
                      decoration: new InputDecoration(
                          /*hintText: "Tujuan",*/
                          labelText: "Tujuan"),  
                      onSaved: (String val) => destination = val,
                    ),
    
                    Padding(
                      padding: const EdgeInsets.all(15.0),
                      child: Center(
                        child: _image == null
                            ? Text('Upload Foto')
                            : Image.file(_image),
                        onSaved: (val) => _image = val,
                      ),
                    ),
                    SizedBox(
                        width: 100.0,
                        height: 100.0,
                        child: RaisedButton(
                          onPressed: getImageFromGallery,
                          child: Icon(Icons.add_a_photo),
                        )),
    
  • Avrilla Wardani
    Avrilla Wardani over 4 years
    It doesnt work, I've tried with FlatButton but onSave still isnt defined
  • Karim Elghamry
    Karim Elghamry over 4 years
    as mentioned in my answer, onPressed property is to be used. there is no onSaved property in FlatButton widget. please refer to the Flutter docs for more info about widgets.