Flutter Web-设置状态 - 文件拾取器 - 文件名 /文件名未显示时,请选择文件挑选文件。 SetState是否正确应用?

发布于 2025-01-22 07:24:36 字数 2047 浏览 0 评论 0原文

我对扑朔迷离和编码非常新。这可能是一个简单的修复程序,但是我找不到任何地方的解决方案或重新配置代码以显示我选择的文件的名称。

我正在尝试创建一个可以接受文档提交的网页。但是,我无法获得代码显示所选择的文件的名称。

但是,打印呼叫正在工作,但是,FileAnameselectected不会更新。

谢谢您的帮助!

import 'dart:html';
import 'dart:io' as io; //type cast

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

import './appbar/appbar.dart';

class UploadFPage extends StatefulWidget {
  const UploadFPage({Key? key}) : super(key: key);

  @override
  State<UploadFPage> createState() => _UploadFPageState();
}

class _UploadFPageState extends State<UploadFPage> {
  UploadTask? task;
  File? file;
  String fileNameSelected = 'No File Selected';

  @override
  Widget build(BuildContext context) {
    final fileName = fileNameSelected;

    return Scaffold(
      appBar: AppBar(
        title: const Text('AppBar'),
      ),
      body: ListView(
        children: [
          Container(
            height: 300,
            color: Colors.amber,
            child: Row(
              children: [
                ElevatedButton.icon(
                  icon: const Icon(Icons.attach_file, size: 28),
                  onPressed: selectFile,
                  label: Text("Attach Invoice"),
                ),
                Text(fileName),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Future selectFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowMultiple: false,
        allowedExtensions: ['pdf']);

    if (result == null) {
      return;
    } else {
      print(result.files.single.name);
      //final name = result.files.single.name;
      //final path = result.files.single.path!;
      //final file = result.files.single.bytes!;

      setState(() {
        String fileNameSelected = basename(result.files.single.name);
        //file = File(size, path);
      });
    }
  }
}

I am very new to flutter and coding. This may be an easy fix but I cannot find a solution anywhere or reconfigure the code to display the name of the file I am picking.

I am trying to create a web page that can accept document submissions. However, I cannot get the code to display the name of the file picked.

The print call is working, however, the fileNameSelected does not update.

Thank you for any help!!

import 'dart:html';
import 'dart:io' as io; //type cast

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

import './appbar/appbar.dart';

class UploadFPage extends StatefulWidget {
  const UploadFPage({Key? key}) : super(key: key);

  @override
  State<UploadFPage> createState() => _UploadFPageState();
}

class _UploadFPageState extends State<UploadFPage> {
  UploadTask? task;
  File? file;
  String fileNameSelected = 'No File Selected';

  @override
  Widget build(BuildContext context) {
    final fileName = fileNameSelected;

    return Scaffold(
      appBar: AppBar(
        title: const Text('AppBar'),
      ),
      body: ListView(
        children: [
          Container(
            height: 300,
            color: Colors.amber,
            child: Row(
              children: [
                ElevatedButton.icon(
                  icon: const Icon(Icons.attach_file, size: 28),
                  onPressed: selectFile,
                  label: Text("Attach Invoice"),
                ),
                Text(fileName),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Future selectFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
        type: FileType.custom,
        allowMultiple: false,
        allowedExtensions: ['pdf']);

    if (result == null) {
      return;
    } else {
      print(result.files.single.name);
      //final name = result.files.single.name;
      //final path = result.files.single.path!;
      //final file = result.files.single.bytes!;

      setState(() {
        String fileNameSelected = basename(result.files.single.name);
        //file = File(size, path);
      });
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-29 07:24:36

更改:

  setState(() {
    String fileNameSelected = basename(result.files.single.name);
  });

通过

  setState(() {
    fileNameSelected = basename(result.files.single.name);
  });

添加varfinal字符串在前方声明新变量(恰好具有与一个名称相同的名称您在上面声明)。您将选择的文件分配给了 - 它迅速脱离了范围。

Change:

  setState(() {
    String fileNameSelected = basename(result.files.single.name);
  });

to

  setState(() {
    fileNameSelected = basename(result.files.single.name);
  });

By adding var, final or String in front you are declaring a new variable (which happens to have the same name as the one you declared above). You assign your picked file to that - and it promptly goes out of scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文