- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试获取某个目录中所有文件的列表。
我从名为 getUserVideos()
的 future 函数中获取文件,如果在函数内部我尝试打印数据,我可以看到结果,但我不能在函数外部使用数据.
class _mediaUtentiState extends State<mediaUtenti> {
var lightBlue = Color.fromRGBO(0, 197, 205, 1.0);
var _imagesDir;
@override
void initState() {
super.initState();
getUsersVideos();
}
List<String> Names = [
'Abhishek',
'John',
'Robert',
'Shyam',
'Sita',
'Gita',
'Nitish'
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: lightBlue,
appBar: new AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(padding: const EdgeInsets.all(8.0), child: Text('Nome')),
Container(
child: CircleAvatar(
backgroundImage: NetworkImage('http://i.pravatar.cc/300'),
),
),
],
),
backgroundColor: purple,
),
body: new Container(
child: new ListView.builder(
reverse: false,
itemBuilder: (_, int index) => EachList(this.Names[index]),
itemCount: this.Names.length,
),
),
);
}
Future<String> getUsersVideos() async {
print('something');
final Directory extDir = await getExternalStorageDirectory();
final String dirPath = '${extDir.path}/Movies/Veople';
final myDir = new Directory(dirPath);
List<FileSystemEntity> _images;
_images = myDir.listSync(recursive: true, followLinks: false);
print(_images.length);
_imagesDir = _images;
}
}
class EachList extends StatelessWidget {
final String name;
EachList(this.name);
@override
Widget build(BuildContext context) {
return new Card(
child: new Container(
padding: EdgeInsets.all(8.0),
child: new Row(
children: <Widget>[
new CircleAvatar(
child: new Text(name[0]),
),
new Padding(padding: EdgeInsets.only(right: 10.0)),
new Text(
name,
style: TextStyle(fontSize: 20.0),
)
],
),
),
);
}
}
现在我只显示名称列表,但我想为路径中的每个文件显示一张卡片。
例如,在函数 getUserVideos()
中,当我尝试打印 imagesDir
时,我得到了正确的结果 [File: '/storage/emulated/0/Movies/Veople/1556217605345.mp4',文件:'/storage/emulated/0/Movies/Veople/1556217605345.png',文件:'/storage/emulated/0/Movies/Veople/1556217632709.mp4',文件:
但我无法以任何方式从该函数访问 _imageDir。
...]
我确定可以用几行代码解决这个问题,但现在已经 3 个小时了,我无法找到解决方案。
谢谢!
最佳答案
我认为这肯定已经得到了回答,但是虽然有很多关于 FutureBuilder 和列表的问题,但没有一个是完全像这样的或者没有得到充分的回答。
我会这样做:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
Future<List<FileSystemEntity>> _getUsersVideos() async {
print('something');
final Directory extDir = await getExternalStorageDirectory();
final String dirPath = '${extDir.path}/Movies/Veople';
final myDir = new Directory(dirPath);
List<FileSystemEntity> _images = myDir.listSync(recursive: true, followLinks: false);
return _images;
}
class ListFromFuture extends StatefulWidget {
@override
_ListFromFutureState createState() => _ListFromFutureState();
}
class _ListFromFutureState extends State<ListFromFuture> {
Future<List<FileSystemEntity>> future;
@override
void initState() {
super.initState();
future = _getUsersVideos();
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: future,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return Container(
alignment: Alignment.center,
child: Text("Loading"),
);
break;
case ConnectionState.done:
if (snapshot.hasError) {
// return whatever you'd do for this case, probably an error
return Container(
alignment: Alignment.center,
child: Text("Error: ${snapshot.error}"),
);
}
var data = snapshot.data;
return new ListView.builder(
reverse: false,
itemBuilder: (_, int index) => EachList(data[index]),
itemCount: data.length,
);
break;
}
},
);
}
}
其中的重要部分是:
老实说,您的示例实际上非常接近于正常运行。您所要做的就是将设置 _imagesDir = images
的行包装在 setState(() => ...)
中,它应该可以工作(假设列表不会返回空)。不过,您还应该检查 _imagesDir == null,否则您可能会遇到空指针异常。
关于asynchronous - 如何使用 future 的列表并在 listView 中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55862681/
来自 Akka 文档,Pipelining and Parallelism Akka Streams processing stages (be it simple operators on Flow
我正在开发一个提取元数据的 chrome 扩展程序。解析元数据的代码包含在内容脚本中。 background.js 和 content.js 通过 sendMessage 请求和响应进行通信。我遇到了
我正在使用 Python 3.7.4 和这段代码(MWE): import asyncio async def foo(x): await asyncio.sleep(1) retur
嘿,我对 Dart Futures 很陌生,我有以下情况。 每当用户在 UI 中键入一个字母时,addressChanged()我的 ui_component 中的方法被调用。该方法调用方法getPr
我在尝试将异步函数转换为同步函数时遇到问题。 这是类中的一个方法: doPost: function(call, data) { var uri = 'http://localhost/api
在很多关于 C# 的 async/await 的讨论中,我看到人们提到了“自然异步”或“纯异步”的术语。这些术语到底是什么意思? “自然异步”操作的一些示例是什么?为什么这样调用它? “非自然异步”操
现在我正在使用 Gevent,我想问两个问题: 有没有办法执行特定的任务,这将 从不异步执行 (而不是在每个任务中使用锁) 有没有办法到优先在 Gevent 中生成任务?就像一组将以低优先级生成的任务
在 document , 如果方法也用@gen.coroutine 修饰,则不需要@web.asynchronous。像这样 @web.asynchronous @gen.coroutine def
已在 Google Analytics(分析)帮助论坛中发布此内容,但无人能提供帮助。希望我在这里有更多的运气......: 我对我的网页使用 Google Anlaytics 异步跟踪。像个魔法一样
我正在尝试从 Controller 异步发送电子邮件并收到以下错误: 我不想等待电子邮件发送完成操作。 An asynchronous module or handler completed whil
在使用 SendMailAsync 时出现以下错误: An asynchronous module or handler completed while an asynchronous operati
我有一个非常简单的 ASP.NET MVC 4 Controller : public class HomeController : Controller { private const st
我正在编写一个使用 ASP.NET Web API 代理一些 HTTP 请求的应用程序,我正在努力识别间歇性错误的来源。这似乎是一个竞争条件...但我不完全确定。 在详细介绍之前,先介绍应用程序的一般
Cancel CancellationTokenSource 的成员对象“传达取消请求”,我认为这意味着它是触发并忘记并且不会等到取消完成(例如,所有异常处理程序都已运行)。这很好,但我需要等到一个未
在 D 中异步调用其他进程的首选方法是什么?我的用例正在调用 svn status检查退出状态,并解析其标准输出和错误。 最佳答案 我想 std.stdio.popen是你想要的: void pope
我一直听说使用异步编程模式会使我的代码运行得更快。为什么这是真的?无论是现在运行还是稍后运行,都不是必须以任何一种方式运行完全相同的代码吗? 最佳答案 它不是更快,它只是不浪费时间。 同步代码在等待
我有点困惑为什么同步调用与异步调用不同,因为从来没有“立即”响应,它仍然需要几纳秒或几毫秒? 最佳答案 同步调用在完成其工作(或达到超时)后返回其调用者。异步调用在启动其他事件后立即返回。 这意味着,
我正在尝试使用 MSDN 上描述的 OVERLAPPED 结构异步调用 DeviceIO 函数。 我正在使用 FSCTL_ENUM_USN_DATA 控制代码来枚举 NTFS 驱动器的 MFT,但我无
我一直在尝试创建一个服务器进程,以异步方式从客户端进程接收输入文件路径和输出路径。服务器进行了一些与数据库有关的转换,但是为了简单起见,我们假设它只是将所有内容都转换为大写。这是服务器的一个玩具示例:
我正在编写一个异步方法,它应该异步查询一个端口,直到找到一个,或者在 5 分钟后超时; member this.GetPort(): Async = this._GetPort(DateTim
我是一名优秀的程序员,十分优秀!