dart Flutter 'provider.bloc'的BloC模式返回null

xdnvmnnf  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(112)

我试图在register_page上实现BLoC模式,但当我试图调用上下文时,似乎provider.bloc返回null。尝试了stackoverflow的一些建议,但没有运气。有人想告诉我我的代码有什么问题吗?见下文:

bloc_provider.dart

class BlocProvider<T extends BlocBase> extends StatefulWidget {
  final T bloc;
  final Widget child;

  static Type _typeOf<T>() => T;

  BlocProvider({Key key, @required this.child, this.bloc}) : super(key: key);

  @override
  _BlocProviderState<T> createState() => _BlocProviderState();

  static T of<T extends BlocBase>(BuildContext context) {
    final type = _typeOf<BlocProvider<T>>();
    BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
    return provider.bloc;
  }
}

注册页面.dart

Widget _registerButton(BuildContext context) {
    final UserBloc userBloc = BlocProvider.of<UserBloc>(context); // having an issue in this line
    return Container(
      height: 50,
      width: MediaQuery.of(context).size.width / 1.1,
      child: BlocProvider(
        bloc: UserBloc(),
        child: RaisedButton(
          child: Container(
            alignment: Alignment.center,
            height: 50,
            width: MediaQuery.of(context).size.width,
            child: Text(
              'register'.toUpperCase(),
              style: TextStyle(
                color: getColor(ColorList.WhiteCream, 1.0),
                fontSize: 20,
              ),
            ),
          ),
          onPressed: () {},
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(60),
            side: BorderSide(
              color: getColor(ColorList.WhiteCream, 1.0),
              width: 2.0,
            ),
          ),
          color: getColor(ColorList.DarkGreen, 1.0),
        ),
      ),
    );
  }

其他部分

@override
  Widget build(BuildContext context) {
    return BlocProvider(
      bloc: UserBloc(),
      child: Scaffold(
        appBar: PreferredSize(
          preferredSize: Size.fromHeight(50.0),
          child: AppBar(
            elevation: 0.0,
            backgroundColor: getColor(ColorList.DarkGreen, 1.0),
          ),
        ),
        body: Column(
          children: <Widget>[
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height / 6,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: [
                    getColor(ColorList.DarkGreen, 1.0),
                    getColor(ColorList.LightGreen, 1.0),
                  ],
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                ),
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(90),
                ),
              ),
              child: Stack(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(left: 40.0, bottom: 15.0),
                    child: Container(
                      alignment: Alignment.center,
                      width: 100,
                      height: 100,
                      decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          border: Border.all(
                              color: getColor(ColorList.WhiteCream, 1.0),
                              width: 2.0)),
                      margin: const EdgeInsets.only(top: 32.0),
                      padding: const EdgeInsets.all(3.0),
                      child: Container(
                        height: 70,
                        width: 70,
                        child: ClipOval(
                          child: Image.asset(
                            'assets/images/log-ayuda-black.png',
                            color: getColor(ColorList.WhiteCream, 1.0),
                          ),
                        ),
                      ),
                    ),
                  ),
                  Align(
                    alignment: FractionalOffset.bottomRight,
                    child: Padding(
                      padding: EdgeInsets.only(
                        right: 32,
                        bottom: 15.0,
                      ),
                      child: Text(
                        'Register'.toUpperCase(),
                        style: TextStyle(
                            fontFamily: 'OpenSans',
                            color: getColor(ColorList.WhiteCream, 1.0),
                            fontSize: 25,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              height: MediaQuery.of(context).size.height / 1.41,
              width: MediaQuery.of(context).size.width,
              child: SingleChildScrollView(
                padding: EdgeInsets.only(top: 20.0),
                physics: const ClampingScrollPhysics(),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    // username
                    _usernameTextField(context),
                    _paddingSpace(usernameHasError),
                    // password
                    _passwordTextField(context),
                    _paddingSpace(passwordHasError),
                    // email address
                    _emailAddressTextField(context),
                    _paddingSpace(emailAddressHasError),
                    // Contact Number
                    _contactNumberTextField(context),
                    _paddingSpace(contactNumberHasError),
                    // First name
                    _firstNameTextField(context),
                    _paddingSpace(firstNameHasError),
                    // middle name
                    _middleNameTextField(context),
                    _paddingSpace(false),
                    // last name
                    _lastNameTextField(context),
                    _paddingSpace(lastNameHasError),
                    // gender
                    _buildGenderRadioButton(context),
                    _paddingSpace(selectGenderHasError),
                    // birthdate
                    _buildDateTimePicker(context),
                    selectBirthDateHasError
                        ? Padding(
                            padding: EdgeInsets.only(top: 10, bottom: 10),
                            child: Text(
                              'You must be 18 years old and above',
                              style: TextStyle(color: Colors.redAccent),
                            ),
                          )
                        : _paddingSpace(false),
                    // register button
                    _registerButton(widget.context),
                    _paddingSpace(false),
                  ].where(notNull).toList(),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

这可能是我在Navigator.push上的MaterialPageRoute函数中的问题。请参阅下面的导航代码:

Container(
                  height: 60,
                  width: MediaQuery.of(context).size.width,
                  padding: const EdgeInsets.only(left: 16.0, right: 16.0),
                  child: FlatButton(
                    child: Text(
                      'Not yet a member? Sign up now!',
                      style: TextStyle(
                        fontFamily: 'OpenSans',
                        fontSize: 18.0,
                        color: getColor(ColorList.WhiteCream, 1.0),
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    highlightColor: Colors.transparent,
                    splashColor: Colors.transparent,
                    color: Colors.transparent,
                    onPressed: () {
                      Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => RegisterPage(
                                    context: context,
                                  )));
                    },
                  ),
                ),

我相信我在正确的方式 Package ScaffoldBlocProvider下的 register_page.dart..我不知道为什么它仍然返回null值。

ldioqlga

ldioqlga1#

此处的问题是由于在添加块之前访问了该块而导致的。在Widget build()内部构建它应该可以解决这个问题。

相关问题