是否有一个与Observable.Throttle等效的Streams ?如果不是,是否有任何合理的优雅方法来达到类似效果?
考虑这样的代码:
import 'dart:async';
foo() {
new Timer(onesec, bar);
}
bar() {
throw "from bar";
}
const onesec = const Duration(seconds:1);
main() {
runZoned(() {
new Timer(onesec, foo);
},
onError: (e, stackTrace) => print(stackTrace));
}
Run Code Online (Sandbox Code Playgroud)
如何在打印出来的时候告诉我bar"被叫" 了?foostackTrace
我希望看到类似的东西:
bar
...
foo
...
main
Run Code Online (Sandbox Code Playgroud) IntelliJ或Dart编辑器中的调试器不会在此代码的断点处停止:
main() async {
var x = 1;
bool stop = true; // breakpoint here
}
Run Code Online (Sandbox Code Playgroud)
...但是对于这段代码:
main() {
var x = 1;
bool stop = true; // breakpoint here
}
Run Code Online (Sandbox Code Playgroud)
Dart SDK 1.8.3和两个IDE上的最新稳定版本.
对于像这样的小例子
import 'dart:async';
import 'package:stack_trace/stack_trace.dart';
void main() {
scheduleAsync();
}
void scheduleAsync() {
new Future.delayed(new Duration(seconds: 1))
.then((_) => runAsync());
}
void runAsync() {
throw 'oh no!';
}
Run Code Online (Sandbox Code Playgroud)
我得到了这个堆栈跟踪.我能跟踪回拨的最远的是runAsync()呼叫scheduleAsync().没有信息,scheduleAsync从中调用main.
Unhandled exception: Uncaught Error: oh no! Stack Trace: #0 runAsync (file:///home/myuser/dart/playground/bin/stack_trace/main.dart:14:3) #1 scheduleAsync. (file:///home/myuser/dart/playground/bin/stack_trace/main.dart:10:28) #2 _RootZone.runUnary (dart:async/zone.dart:1155) #3 _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:484) #4 _Future._propagateToListeners (dart:async/future_impl.dart:567) #5 _Future._complete (dart:async/future_impl.dart:348) #6 Future.Future.delayed. (dart:async/future.dart:228) #7 Timer._createTimer. (dart:async-patch/timer_patch.dart:16) #8 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:385) #9 _handleMessage (dart:isolate-patch/timer_impl.dart:411) #10 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:142) #0 _rootHandleUncaughtError. …
考虑以下示例:
Future<int> doAsyncThing() => new Future.value(42);
usingAsync() async => doAsyncThing();
main() {
var thing = usingAsync();
// what is the runtimeType of thing ?
}
Run Code Online (Sandbox Code Playgroud)
返回的对象的runtimeType是什么usingAsync()?是Future<Future<int>>还是Future<int>其他?
我的数据是这样的:
{
"five": {
"group": {
"one": {
"order": 2
},
"six": {
"order": 1
}
},
"name": "Filbert",
"skill": "databases"
},
"four": {
"group": {
"three": {
"order": 2
},
"two": {
"order": 1
}
},
"name": "Robert",
"skill": "big data"
},
"one": {
"name": "Bert",
"skill": "data analysis"
},
"seven": {
"name": "Colbert",
"skill": "data fudging"
},
"six": {
"name": "Ebert",
"skill": "data loss"
},
"three": {
"name": "Gilbert",
"skill": "small data"
},
"two": {
"name": "Albert",
"skill": …Run Code Online (Sandbox Code Playgroud) 我需要提供一个功能列表;但是,我想从回调函数中产生列表,该回调函数本身在主函数内部-这导致yield语句不是针对主函数执行,而是针对回调函数执行。
我的问题与此处解决的问题非常相似:Dart组件:如何返回异步回调的结果?但是我不能使用完成器,因为我需要屈服而不返回。
下面的代码应更好地描述问题:
Stream<List<EventModel>> fetchEvents() async* { //function [1]
Firestore.instance
.collection('events')
.getDocuments()
.asStream()
.listen((snapshot) async* { //function [2]
List<EventModel> list = List();
snapshot.documents.forEach((document) {
list.add(EventModel.fromJson(document.data));
});
yield list; //This is where my problem lies - I need to yield for function [1] not [2]
});
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个服务器端Dart类,该类执行各种与数据相关的任务。所有这些任务都依赖于首先初始化的数据库。问题在于数据库的初始化是异步发生的(返回Future)。我首先尝试将初始化代码放入构造函数中,但是由于似乎不可行,因此放弃了这种方法。
我现在正在尝试找出如何强制进行数据库初始化,这是访问数据的任何方法调用的第一步。因此,换句话说,当在下面调用tryLogin()时,我想首先检查数据库是否已初始化,并在必要时对其进行初始化。
但是,有两个障碍。如果尚未初始化数据库,则代码很简单-初始化db,然后使用返回的future的then()方法完成其余功能。如果数据库尚未初始化,那么将then()方法附加到什么?
第二个相关问题是,当当前正在初始化数据库但此过程尚未完成时会发生什么?我如何才能收回并返回这个“进行中”的未来?
这是我要处理的代码的基本要点:
class DataManager {
bool DbIsReady = false;
bool InitializingDb = false;
Db _db;
Future InitMongoDB() {
print("Initializing MongoDB");
InitializingDb = true;
_db = new Db("mongodb://127.0.0.1/test");
return _db.open().then((_) {
DbIsReady = true;
InitializingDb = false;
});
}
Future<List> attemptLogin(String username, String password) {
Future firstStep;
if ((!DbIsReady) && (!InitializingDb) {
Future firstStep = InitMongoDB()
}
else if (InitializingDb) {
// Need to return the InitMongoDB() Future that's currently running, but how?
}
else {
// How …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以在继续处理之前等待回调。
我正在使用一个在内部处理 future 的库,如果成功,则进行回调,否则在内部处理错误而不进行回调。
现在我尝试使用这个库创建一个实例,然后用随机测试数据填充它,然后更新该实体。
Map generateRandomizedInstance() {
lib.createEntity((result1){
result1["a"] = generateRandomA();
result1["b"] = generateRandomB();
result1["c"] = generateRandomC();
...
lib.updateEntity(result1, (result2){
// want to return this result2
return result2;
})
});
}
Run Code Online (Sandbox Code Playgroud)
如果我只创建一个实体并更新一次,那就没问题了,但我想创建大量随机数据:
ButtonElement b = querySelector("button.create")..onClick.listen((e){
for (int i = 0; i < 500; i++) {
generateRandomizedInstance();
}
});
Run Code Online (Sandbox Code Playgroud)
由于回调返回的速度不够快,这段代码很快就会崩溃。
我尝试将方法签名更改为
generateRandomizedInstance() async {
Run Code Online (Sandbox Code Playgroud)
然后做:
for (int i = 0; i < 500; i++) {
print(await generateRandomizedInstance());
}
Run Code Online (Sandbox Code Playgroud)
但该等待语法似乎无效,并且我不完全确定如何在某种将来包装该回调代码,以便我可以等待回调返回,然后再继续循环的下一次迭代。
我在末尾尝试了一个 while 循环,generateRandomizedInstance等待结果变量不为空,但这会杀死浏览器,并且看到我并不总是得到回调,在某些情况下它可能会导致无限循环。
关于如何在等待回调时暂停 for 循环的任何想法/建议?
我需要将文本写入“.txt”文件中,将其保存在变量中并将其赋予Text, 内的TextField. 这个想法是将用户输入写入“.txt”文件中,以便他可以在需要时在 .txt 上读取他所写的内容TextField。
一切正常,当我读取文件时,它会获取正确的内容,但是当我将其存储在变量中以便Text(var_name...)很好地使用它时,我在屏幕上读到的是“‘未来’的实例”。
我知道这个问题来自于对异步和未来的错误处理,但我想真正理解为什么这不起作用。
这是我的代码:
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get _localBio async {
final path = await _localPath;
print(path);
return File('$path/bio.txt');
}
Future<File> _write(String text, String filename) async {
final file = await _localBio;
// Write the file.
return file.writeAsString(text);
}
Future<String> _read() async {
try {
final file = await _localBio;
String body = await file.readAsString();
// Read …Run Code Online (Sandbox Code Playgroud)