type 'MappedListIterable<File, Stack>' is not a subtype of type 'List<Widget>'

来源:6-16 拍照APP开发-图片获取与图片展示【实战尝鲜】

慕神2393367

2021-07-16

图片描述

import 'dart:io';

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

class PhotoImage extends StatefulWidget {
  @override
  _PhotoImageState createState() => _PhotoImageState();
}

class _PhotoImageState extends State<PhotoImage> {
  List<File> _images = [];

  Future getImage(bool isTakePhoto) async {
    // 点击完关闭弹框
    Navigator.pop(context);
    var image = await ImagePicker.pickImage(
        source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);

    print(image);

    setState(() {
      _images.add(image);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: Text('Image Picker Example'),
      ),
      body: Center(
        child: Wrap(
          spacing: 5,
          runSpacing: 5,
          children: _getImages(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _pickImage,
        tooltip: '选择图片',
        child: Icon(Icons.add_a_photo),
      ),
    ));
  }

  _pickImage() {
    showModalBottomSheet(
        context: context,
        builder: (context) => Container(
            height: 160,
            child: Column(
              children: [
                _item('拍照', true, Icons.camera),
                _item('从相册中选择', false, Icons.photo_library)
              ],
            )));
  }

  _item(String title, bool isTakePhoto, IconData iconType) {
    return GestureDetector(
      child: ListTile(
        leading: Icon(iconType),
        title: Text(title),
        onTap: () => getImage(isTakePhoto),
      ),
    );
  }

  _getImages() {
    return _images.map((image) {
      return Stack(
        children: <Widget>[
          ClipRRect(
              child: Image.file(
            image,
            width: 120,
            height: 90,
            fit: BoxFit.fill,
          ))
        ],
      );
    });
  }
}
写回答

1回答

CrazyCodeBoy

2021-07-18

你_getImages返回的是map需要在方法尾部加个toList()将map转成list

0
0

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3270 问题

查看课程