当StreamBuilder中的变量更改时如何重建屏幕

时间:2020-07-13 06:47:44

标签: flutter

我正在为Flutter应用编写登录页面。我使用StreamBuilder来访问来自websocket的数据。我解码json,并想更新确定用户是否通过身份验证的变量。通过用户身份验证后,我想重建屏幕,然后向用户显示新屏幕。我尝试调用setState((){});在StreamBuilder中,但出现setState错误。 这是我的代码:

@override
  Widget build(BuildContext context) {
    return (authenticated == 0)
    ? Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.light,
        child: GestureDetector(
          onTap: () => FocusScope.of(context).unfocus(),
          child: Stack(
            children: <Widget>[
              Container(
                height: double.infinity,
                width: double.infinity,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Color(0xFF73AEF5),
                      Color(0xFF61A4F1),
                      Color(0xFF478DE0),
                      Color(0xFF398AE5),
                    ],
                    stops: [0.1, 0.4, 0.7, 0.9],
                  ),
                ),
              ),
              Container(
                height: double.infinity,
                child: SingleChildScrollView(
                  physics: AlwaysScrollableScrollPhysics(),
                  padding: EdgeInsets.symmetric(
                    horizontal: 40.0,
                    vertical: 120.0,
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        'SIARS',
                        style: TextStyle(
                          color: Colors.white,
                          fontFamily: 'OpenSans',
                          fontSize: 30.0,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      SizedBox(height: 30.0),
                      _buildEmailTF(),
                      SizedBox(
                        height: 30.0,
                      ),
                      _buildPasswordTF(),
                      _buildForgotPasswordBtn(),
                      _buildLoginBtn(),
                      _buildSignupBtn(),
                      StreamBuilder(
                        stream: channel.stream,
                        builder: (context, snapshot) {
                          print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
                          if(snapshot.hasData)
                          {
                            String rawJson = snapshot.data;
                            Map<String, dynamic> inputMap = jsonDecode(rawJson);
                            print('RAWJSON: $rawJson');
                            if(inputMap['type'] == 'auth_response')  authenticated = inputMap['access'];
                            print('authentication: $authenticated');
                            setState(() {}); // ERROR
                          }
                          return (Text(''));
                        },
                      ),
                    ],
                  ),
                ),
              )
            ],
          ),
        ),
      )
    )
    : StreamBuilder<BluetoothState>(
        stream: FlutterBlue.instance.state,
        initialData: BluetoothState.unknown,
        builder: (c, snapshot) {
          final state = snapshot.data;
          if (state == BluetoothState.on) return FindDevicesScreen();
          return BluetoothOffScreen(state: state);
        }
    );
  }
}

0 个答案:

没有答案