我在我的代码中使用了 FutureBuilder 来查询 Firestore 但我一直没有从我在 FutureBuilder。但有趣的是,函数正在执行并获取数据。它只是没有将它传递给 FutureBuilder。
代码:
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:garuda_academy_app/Tools/FixedColors.dart';
class CoursePage extends StatefulWidget {
CoursePage({this.userEmail});
final String userEmail;
@override
_CoursePageState createState() => _CoursePageState();
}
class _CoursePageState extends State<CoursePage> {
@override
void initState() {
super.initState();
}
Future<List<DocumentSnapshot>> _getCourses() async {
List<DocumentSnapshot> userCourses = [];
await Firestore.instance
.collection("Users")
.document(widget.userEmail)
.get()
.then((DocumentSnapshot userDetails) {
List<dynamic> courses = userDetails["Courses"];
courses.forEach((dynamic course) {
Firestore.instance
.collection("Courses")
.document(course)
.get()
.then((DocumentSnapshot courseDetails) {
userCourses.add(courseDetails);
print(userCourses.length);
});
});
});
return userCourses;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: secondaryColor,
resizeToAvoidBottomInset: false,
body: FutureBuilder(
future: _getCourses(),
builder: (context, AsyncSnapshot<List<DocumentSnapshot>> snapshot) {
// loading display
if (snapshot.hasError ||
(snapshot.connectionState == ConnectionState.waiting)) {
if (snapshot.hasError) print('${snapshot.error}');
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(primaryColor),
),
);
}
print(snapshot.data.length);
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Text(
snapshot.data[index].documentID,
style: TextStyle(color: Colors.black),
);
},
);
},
),
);
}
}
注意我打印了两次数据长度来检查我是否得到任何东西,输出是这样的:
0 // the future print
1 // the function print
似乎 await 没有正常运行,因为 future 似乎在 await 之前采用了 List。
有人知道为什么会这样吗?
最佳答案
问题是您在 FutureBuilder Widget 内部调用异步方法。在 initState 方法中调用该函数并在一个变量中获取该返回值并在 FutureBuilder Widget 中使用它。
Check this link for more understanding
Future<List<DocumentSnapshot>> _course;
void initState()
{
super.initState();
course= _getCourses()
}
在 FutureBuilder Widget 中使用 course 变量
FutureBuilder(
future: _course,
builder: write your code)
关于flutter - Future builder 没有获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57019109/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit