简体   繁体   中英

FileImage can't be assigned to parameter

So I've trying to make multiple upload picture by having these:

_selectImage(ImagePicker().getImage(source: ImageSource.gallery), 1);
_selectImage(ImagePicker().getImage(source: ImageSource.gallery), 2);
_selectImage(ImagePicker().getImage(source: ImageSource.gallery), 3);

When method of this:

    void _selectImage(Future<PickedFile> image, int imageNumber) async{
    FileImage tempImg = (await image) as FileImage;
    switch(imageNumber){
      case 1: setState(() => _image1 = tempImg);
      break;

      case 2: setState(() => _image3 = tempImg);
      break;

      case 2: setState(() => _image3 = tempImg);
      break;
    }
  }

but it says 'FileImage' can't be assigned to parameter type 'File' at here:

    Widget _displayChild() {
    if (_image1 == null){
      return Padding(
        padding: const EdgeInsets.fromLTRB(15.0, 20.0, 15.0, 20.0),
        child: new Icon(Icons.add, color: grey,),
      );
    }else{
      return Padding(
        padding: const EdgeInsets.fromLTRB(15.0, 20.0, 15.0, 20.0),
        child: Image.file(_image1)
      );
    }
  }

Can any1 help? This newer version confuses me

It seems you maybe misunderstand the file that should be assigned so I took a few minutes to implement this into a quick main.dart file and get it working for you.

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(
    MaterialApp(
      home: MyWidget(),
    ),
  );
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  File _image1;
  File _image2;
  File _image3;

  void _selectImage(int imageNumber) async {
    final PickedFile pickedFile = await ImagePicker().getImage(source: ImageSource.gallery);

    switch (imageNumber) {
      case 1:
        setState(() => _image1 = File(pickedFile.path));
        break;
      case 2:
        setState(() => _image3 = File(pickedFile.path));
        break;
      case 2:
        setState(() => _image3 = File(pickedFile.path));
        break;
    }
  }

  Widget _displayChild() {
    if (_image1 == null) {
      return RaisedButton(
        padding: const EdgeInsets.fromLTRB(15.0, 20.0, 15.0, 20.0),
        child: new Icon(Icons.add, color: Colors.blue),
        onPressed: () => _selectImage(1),
      );
    } else {
      return Padding(
        padding: const EdgeInsets.fromLTRB(15.0, 20.0, 15.0, 20.0),
        child: Image.file(_image1),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Build some widgets!',
              style: Theme.of(context).textTheme.headline4,
            ),
            _displayChild(),
          ],
        ),
      ),
    );
  }
}

Loading an image from a file creates an in memory copy of the file, which is retained in the ImageCache. The underlying file is not monitored for changes. If it does change, the application should evict the entry from the ImageCache.

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