我有一个应用程序,您需要登录才能继续(例如使用Google).
我想在需要验证时重定向用户.
但是,当我跑一个Navigator.of(context).pushNamed("myroute").我收到以下错误:
??? EXCEPTION CAUGHT BY WIDGETS LIBRARY ????????????????????????????????????????????????????????????
I/flutter ( 5624): The following assertion was thrown building _ModalScopeStatus(active):
I/flutter ( 5624): setState() or markNeedsBuild() called during build.
I/flutter ( 5624): This Overlay widget cannot be marked as needing to build because the framework is already in the
I/flutter ( 5624): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter ( 5624): only if one of its …Run Code Online (Sandbox Code Playgroud) 如/sf/answers/3462080261/BuildContext中所示,在 Flutter 中初始渲染小部件后(即在 中initState),本质上有两种访问方式:
Future.delayed(Duration.zero, () {
// context can be used here...
});
Run Code Online (Sandbox Code Playgroud)
和
SchedulerBinding.instance.addPostFrameCallback((_) {
// context can be used here...
});
Run Code Online (Sandbox Code Playgroud)
这两种方法有什么区别?使用一种方法相对于另一种方法有什么优势吗?是否有任何我没有看到的隐藏副作用?
根据我自己的探索,如果我在同一个小部件中使用这两种方法initState,则执行回调的顺序与注册回调的顺序相同。我查看了 Flutter 源代码Future.delayed,SchedulerBinding.instance.addPostFrameCallback但我不太明白发生了什么。
我在 FutureBuilder 启动两次时遇到问题。首先它正确获取数据,返回我的 StartScreen,然后几秒钟后,StartScreen 重建,我注意到 FutureBuilder 再次触发。
这是我的代码,它非常简单,所以我想知道可能是什么问题?!?
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FirebaseUser user;
@override
void initState() {
// TODO: implement initState
super.initState();
getNewestlocation();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'APP',
theme: buildTheme(),
home: FutureBuilder<FirebaseUser>(
future: Provider.of<AuthService>(context).getUser(),
builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.error != null) {
print('error');
return Text(snapshot.error.toString());
}
user …Run Code Online (Sandbox Code Playgroud) 我有NestedScrollView一个SliverAppBar。的身体NestedScrollView是有一个小部件CustomScrollView用SliverList。
我想从ScrollController给予的偏移量,NestedScrollView但是它只给我偏移量,直到看不见为止SliverAppBar,但是此后,SliverList继续滚动但ScrollController没有给出偏移量。
NestedScrollView(
headerSliverBuilder: (_, __) => SliverAppBar(..),
body: RefreshIndicator(
child: CustomScrollView(..),
),
)
Run Code Online (Sandbox Code Playgroud)
该CustomScrollView会自动使用PrimaryScrollController提供NestedScrollView。
当侦听器附加到该滚动视图时,当SliverAppBar幻灯片滑入和滑出视图时,将调用该侦听器,直到position.extentAfter等于0.0,然后再也不会更新,无论您向下滚动多远,因为extentAfter始终保持在0.0。
如何获取CustomScrollView内部的滚动位置NestedScrollView?
之所以重要,PrimaryScrollController是因为ScrollController应当具有的功能PrimaryScrollController。无论如何,为了SliverAppBar使它起作用(即,它与内容一起滚动出视图),该NestedScrollView controller参数需要以某种方式与CustomScrollView controller(或primary)参数匹配。
任何允许内容滚动的实现都可以PrimaryScrollController.of(context)。 …
在我们现在正在编写的应用程序中,我们遇到了一个问题,如果将焦点放在底部工作表内的文本字段,键盘会在短暂闪烁后消失。
经过数小时的尝试修复,我试图隔离这种奇怪的行为并提出以下错误:
如果我点击非主页的页面脚手架底部工作表内的 TextField,键盘会消失。(见样本)
我已经在https://github.com/flutter/flutter/issues/36271上发布了这个,但还没有得到回应。
有人有想法吗?这真的是一个错误还是我在路线上做错了什么?
先感谢您。马库斯
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
'secondPage': (_) => SecondPage(),
},
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold( …Run Code Online (Sandbox Code Playgroud) 我需要一些帮助来理解如何从继承的小部件获取数据。
我通常使用直接从构建方法从我的小部件获取参数
@override
Widget build(BuildContext context) {
//THIS METHOD
var data = StateContainer.of(context).data;
return Container(child:Text("${data.parameter}"));
}
Run Code Online (Sandbox Code Playgroud)
但是这个方法不能从 initState 调用,因为还没有 buildContext 。
我需要在 initState 方法中使用该参数(我在其中调用了从服务器获取的数据,并且需要将该数据传递给我的函数),那么,我该怎么做呢?
@override
void initState() {
otherData = fetchData(data);
super.initState();
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用 didChangeDipendencies() 但每次重建视图时都会调用它(从屏幕弹出等),所以它不是我想要使用的,也不是 FutureBuilder 小部件。
有什么建议吗?