簡體   English   中英

如何使用Image Picker在Flutter中上傳圖像

[英]How to upload image in Flutter using Image Picker

我嘗試使用圖像選擇器將圖像保存為圖像上傳表單,我使用了“ OnSaved”,但出現錯誤消息“未定義命名參數OnSaved。如何解決此問題?

這是我使用的代碼:

onSaved: (val) => _image = val,

這是完整的代碼:

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

Center小部件中沒有onSaved屬性。 您可以改用按鈕​​,並使用onPressed屬性或您喜歡的任何其他方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM