简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM