Avrilla Wardani
Avrilla Wardani

Reputation: 37

How to upload image in Flutter using Image Picker

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),
                    )),

Upvotes: 2

Views: 2121

Answers (1)

Karim Elghamry
Karim Elghamry

Reputation: 1421

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.

Upvotes: 1

Related Questions