[英]Checking the existing file in flutter using file picker?
I am working with flutter. I implement filePicker(), to choose files and show them in a listView.我正在使用 flutter。我实现了 filePicker(),以选择文件并将它们显示在 listView 中。 I want to know how we check the already existing file in a list.
我想知道我们如何检查列表中已经存在的文件。 So, the file picker can't add a previously existing file again.
因此,文件选择器无法再次添加以前存在的文件。 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.add(result.files.first);
setState(() {});
},
),
],
),
body: Container(
width: 420,
height: 300,
child: show(
files: files,
),
),
);}}
you can delete directly according to the index list that is intended,您可以直接根据预期的索引列表进行删除,
one of the examples I use我使用的例子之一
Future<void> deleteList(idx) async { print('delete'); _classList.removeAt(idx); print(_classListSection); // images.clear(); String error = 'No Error Detected'; setState(() { _classList = _classList; _count--; // _error = error; }); }
in your case this is the following list "files";在您的情况下,这是以下列表“文件”;
Here we save file location in the item
list after checking the file exists.这里我们在检查文件是否存在后将文件位置保存在
item
列表中。 if a file exists we show the snackbar
widget.如果文件存在,我们会显示
snackbar
小部件。
List item = [];
List<Widget> widgets = [];
Future<void> Filepick(context) async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
//here the checking works---------------------->
var path2 = result.files.single.path;
if (!item.contains(path2)) {
item.add(path2);
File file = File(path2 ?? "");
setState(() {
widgets
.add(SizedBox(height: 100, width: 100, child: Image.file(file)));
});
} else {
// Scaffold.of(globalkey2.currentContext??context)
globalkey2.currentState
?.showSnackBar(SnackBar(content: Text("File Already Exist")));
}
} else {
// User canceled the picker
}
}
FullCode全码
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MaterialApp( home: sta()));
}
class sta extends StatefulWidget {
sta({Key? key}) : super(key: key);
@override
State<sta> createState() => _staState();
}
class _staState extends State<sta> {
GlobalKey<ScaffoldState> globalkey2 =
GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: globalkey2,
appBar: AppBar(),
body: Container(
height: 750,
child: Column(
// mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Align(
alignment: Alignment.center,
child: Container(
height: 100,
width: 100,
child: OutlinedButton(
onPressed: () {
Filepick(context);
},
child: Text("Pick File"),
),
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height - 200,
child: GridView.count(
mainAxisSpacing: 4,
crossAxisCount: 2,
children: [...widgets],
),
),
// ...widgets
],
),
));
}
List item = [];
List<Widget> widgets = [];
Future<void> Filepick(context) async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
var path2 = result.files.single.path;
if (!item.contains(path2)) {
item.add(path2);
File file = File(path2 ?? "");
setState(() {
widgets
.add(SizedBox(height: 100, width: 100, child: Image.file(file)));
});
} else {
// Scaffold.of(globalkey2.currentContext??context)
globalkey2.currentState
?.showSnackBar(SnackBar(content: Text("File Already Exist")));
}
} else {
// User canceled the picker
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.