flutter - setState()在构造函数中调用,可以从另一个类更改窗口小部件的状态吗?

标签 flutter dart

当我尝试通过定义用于设置状态的自定义函数来更改窗口小部件的状态时出现此错误,然后从我创建的新类中调用该函数。

setState() called in constructor: _MyHomePageState#da7cc(lifecycle state: created, no widget, not mounted)



主页:
import 'package:flutter/material.dart';
import 'package:ed_cell/auth_service.dart';
import 'package:flutter/scheduler.dart';

Widget authStatus = Text('Welcome');

class MyHomePage extends StatefulWidget {
  _MyHomePageState myHomePageState= new _MyHomePageState();
  @override
  _MyHomePageState createState() => myHomePageState;
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Container(
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(10)),
          height: 575,
          child: Row(
            children: <Widget>[
              Column(
                children: [
                  Expanded(
                    child: Image.asset(
                      'assets/images/rocket.png',
                    ),
                  ),
                ],
              ),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Text(
                      'U create\nWe support',
                      style:
                          TextStyle(fontSize: 80, fontWeight: FontWeight.bold),
                    ),
                    authStatus,
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  void onAuthChange() {
     var x=AuthService().status;
    if (AuthService().status == 'Sucessful Sign In') {
      setState(()  {
        authStatus = Text(
          'Welcome',
          style: TextStyle(color: Colors.grey, fontSize: 50),
        );
        print(x);
      });
      print('object'+x);
    } else {
      setState(()  {
        authStatus = RaisedButton(
          onPressed: null,
          color: Colors.deepOrange,
          child: Text(
            'Join Now',
            style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
          ),
        );
      });
    }
  }
}

AuthService:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:ed_cell/home_page.dart';

class AuthService{
  String status;

  signIn(email,password){
    FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password).then((user){
      print('Signed In '+user.user.uid);
      status='Sucessful Sign In';
    }).catchError((e){
      status='Failure in Sign In';
      print(e);
    });
    MyHomePage().myHomePageState.onAuthChange();
  }
}

最佳答案

该错误与HomePage尚未插入小部件树有关。您可以按照正确的方法从另一个类调用setState,但是HomePage窗口小部件必须位于窗口小部件树中的某个位置,才能执行setState

关于flutter - setState()在构造函数中调用,可以从另一个类更改窗口小部件的状态吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009129/

相关文章:

flutter - 为什么此 future 函数返回空列表抖动

flutter - 有没有办法在最初显示表单时执行 TextFormField 上的 InputFormatters?

asynchronous - Flutter Future<dynamic> 与 Future<String> 子类型错误?

android - flutter 的android应用程序在 Release模式下卡住在启动屏幕上

ios - 从移动浏览器(Safari、iOS 上的 Chrome)中的 Flutter Web 应用重定向到移动应用

generics - 从dart中的静态函数访问类中的类型参数的解决方法

flutter - 编译时间错误 : Type arguments in partial instantiations must be instantiated and are therefore not allowed to depend on type parameters

dart - 通过 Dart 中的反射获取私有(private)变量

firebase - 如何将 Firestore 文档列表打印到控制台?

dart - 是否可以在运行时获取 pubspec.yaml 版本?