【问题标题】:setState() called in constructor, Can I change the state of a widget from another class?setState() 在构造函数中调用,我可以从另一个类更改小部件的状态吗?
【发布时间】:2020-05-25 19:11:17
【问题描述】:

当我试图通过定义一个自定义函数来设置状态来更改小部件的状态时,我遇到了这个错误,然后我从我创建的新类中调用该函数。

setState() 在构造函数中调用:_MyHomePageState#da7cc(lifecycle 状态:已创建,没有小部件,未安装)

主页:

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();
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    该错误与 HomePage 尚未插入到小部件树中有关。您可以从另一个类调用setState,因为您已经正确完成了,但是HomePage 小部件必须已经在小部件树中的某个位置才能执行setState

    【讨论】:

      猜你喜欢
      • 2010-11-02
      • 2010-09-23
      • 2019-12-17
      • 2021-07-20
      • 2020-10-19
      • 1970-01-01
      • 2017-09-04
      • 2021-01-01
      相关资源
      最近更新 更多