Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Showing picked files in list view in flutter

I am working with flutter. I implement filePicker(), to choose files and show them in a listView.But when I pick a new file, listview removes the previous files and shows only the new one. The code is attached below. I am glad if someone helps.

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

class pickFile extends StatefulWidget {
const pickFile({Key? key}) : super(key: key);
@override
State<pickFile> createState() => _pickFileState();
}
class _pickFileState extends State<pickFile> {
List<PlatformFile> files = [];

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text("Add"),
    actions: [
      IconButton(
        icon: const Icon(Icons.add),
        onPressed: () async {
          final result = await FilePicker.platform
              .pickFiles(withReadStream: true, allowMultiple: true);

          if (result == null) return;
          files = result.files;
          setState(() {});
        },
      ),
    ],
  ),
  body: Container(
    color: Colors.blue,
    width: 420,
    height: 200,
    child: show(files: files),
  ),
);}}

//////////////////////////////////////////////////////////

 import 'package:file_picker/src/platform_file.dart';
 import 'package:flutter/material.dart';

 Widget show({
 required List<PlatformFile> files,
 }) {
 return ListView.builder(
 itemCount: files.length,
 itemBuilder: (context, index) {
  final file = files[index];
  return buildFile(file);
 },
 );}
Widget buildFile(PlatformFile file) {
final kb = file.size / 1024;
final mb = kb / 1024;
final size =
  (mb >= 1) ? '${mb.toStringAsFixed(2)} MB' : '${kb.toStringAsFixed(2)} KB';
return Container(
color: Colors.amber,
child: InkWell(
  onTap: () => null,
  child: Container(
    height: 100,
    width: 200,
    color: Colors.red,
    child: ListTile(
      leading: (file.extension == 'jpg' || file.extension == 'png')
          ? Image.file(
              File(file.path.toString()),
              width: 80,
              height: 80,
            )
          : Container(
              width: 80,
              height: 80,
            ),
      title: Text('${file.name}'),
      subtitle: Text('${file.extension}'),
      trailing: Text(
        '$size',
        style: TextStyle(fontWeight: FontWeight.w700),
      ),
    ),
  ),
 ),);}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

that because you every time when to add a file you set a new value to the list
and remove the old files

,
you need to add the file to existing list like this code below

 onPressed: () async {
  final result = await FilePicker.platform
      .pickFiles(withReadStream: true, allowMultiple: true);

  if (result == null) return;
  files.add(result.files);
  setState(() {});
},
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading