有人请解释一下streambuilder和futurebuilder之间的主要区别.我真的很困惑.
使用什么以及何时使用?
他们打算执行哪些任务?
他们每个人如何监听动态列表中的更改?
我的屏幕上有一个listView.我已经附加了一个控制器.我可以调用我的端点,接收响应,解析它并插入行.ListView应该自动滚动.确实如此,但并非完美无缺.我总是背后的一个项目.这是我的代码:
@override
Widget build(BuildContext context) {
// Scroll to the most recent item
if (equationList.length > 0) {
_toEnd();
}
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: EquList(equationList, _scrollController),
floatingActionButton: new FloatingActionButton(
onPressed: onFabClick,
tooltip: 'Fetch Post',
child: new Icon(isLoading ? Icons.pause : Icons.play_arrow),
),
);
}
void _toEnd() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(milliseconds: 250),
curve: Curves.ease,
);
}
Run Code Online (Sandbox Code Playgroud)
问题是,我_toEnd()在最后一项插入列表之前调用函数.所以,我正在寻找一个回调(如果有的话)告诉我build()完成了.然后我打电话给我的_toEnd()功能.
在这种情况下,最佳做法是什么?
initState() 在我路由到的第一个 Widget 上被调用两次。
我已经删除了 initState() 方法中正在完成的所有方法调用和工作,以排除它以某种方式调用自身的任何可能性。它所做的只是调用 super.initState()。
路线.dart:
final routes = {
'/login' : (BuildContext context) => new LoginPage(),
'/' : (BuildContext context) => new LoginPage()
};
Run Code Online (Sandbox Code Playgroud)
主要.dart:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Personnel Ledger',
initialRoute: '/login',
routes: routes,
theme: ThemeData(
scaffoldBackgroundColor: Color(0xFF30778B)
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
登录页面.dart:
class LoginPage extends StatefulWidget {
@override
LoginPageState createState() => LoginPageState();
}
class LoginPageState extends State<LoginPage> {
TextEditingController emailTextfieldCtrl;
TextEditingController …Run Code Online (Sandbox Code Playgroud) setState()方法应该被称为的内部initState()方法StatefullWidget吗?
我的理解是该initState()方法将自动应用状态。
下面的代码不起作用。post对象的值为空。
@override
void initState() {
ItemService.getItemById(widget.postId).then((DocumentSnapshot doc){
post = ItemService.getPostFromDocument(doc);
});
}
Run Code Online (Sandbox Code Playgroud)
但是下面的作品。
@override
void initState() {
ItemService.getItemById(widget.postId).then((DocumentSnapshot doc){
setState((){
post = ItemService.getPostFromDocument(doc);
});
});
}
Run Code Online (Sandbox Code Playgroud)
在其他一些情况下,即使不使用setState()同一类也可以正常工作。
因此,什么时候应该使用setState()内部initState()方法,什么时候不应该使用?
另一个相关问题:
super.initState()我initState()什么时候应该打电话给我?我不打扰没关系吗?
我从服务器获取一个 int Id,它在颤振的 HTTP 请求中,这没问题,我想将此值保存在一个变量中,但是当我调用返回值时,我收到此错误实例 'Future' ,有什么建议吗? ? 这是代码
child: RaisedButton(onPressed:() {
CurrentPrjId= SaveProject();
print("CurrentPrjId Is saved CurrentPrjId :::::::: " );
print(CurrentPrjId);
Navigator.of(context).pushReplacementNamed('/AllUSerStories');
}
//Save Project
Future SaveProject() async {
//SaveProjectResult has the id of the current created project which i need that for its user stories
var SaveProjectResult = await GetPostProjects().createProject(_ProjectnameController.text, _CustomernameController.text, _dropNewItem, _dropNewItem2, _StartDateController.text, _EndDateController.text,);
print(":::::::::::::::::::::::::::::::The Project Is Created And Here Its ID In SaveProject:::::::::");
print(SaveProjectResult);
return SaveProjectResult;
Run Code Online (Sandbox Code Playgroud)
}