Bloc: Unable to access status isSubmissionSuccess

Created on 1 Jul 2020  路  2Comments  路  Source: felangel/bloc

@felangel , I am trying to implement the idea of form using flutter_bloc with formz package. But I am unable to access submissionSuccess status code. even if I am returning it from an event. look at my code:

BlocListener is where I am trying to access form state. I am adding a screenshot for your reference.

Screenshot 2020-07-01 at 10 33 13 AM

My main page:

class LogInPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
          leading: Container(
              margin: EdgeInsets.all(2),
              child: Image.asset('icons/cheruvu_logo.png', fit: BoxFit.cover)),
          title: Text('Cheruvu')),
      body: BlocProvider(
        create: (context) => LoginBloc(),
        child: Stack(
          children: <Widget>[BackGroundWidget(), LogInWidget()],
        ),
      ),
    );
  }
}

class BackGroundWidget extends StatelessWidget {
  const BackGroundWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).accentColor,
    );
  }
}

class LogInWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return BlocListener<LoginBloc, LoginState>(
      listener: (context, state) {
        **if (state.status.submissionSuccess) {}**
      },
      child: LogInForm(),
    );
  }
}

class LogInForm extends StatelessWidget {
  const LogInForm({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(8),
      child: Wrap(
        children: <Widget>[
          Container(
            height: 50,
            child: Center(
              child: Text(LocaleKeys.hello_there.tr()),
            ),
          ),
          PhoneNumberInputWidget(),
          OTPButton(),
        ],
      ),
    );
  }
}

class OTPButton extends StatelessWidget {
  const OTPButton({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<LoginBloc, LoginState>(
      builder: (context, state) {
        return Container(
          alignment: Alignment.center,
          margin: EdgeInsets.fromLTRB(8, 2, 8, 2),
          padding: EdgeInsets.all(4),
          child: RaisedButton(
            onPressed: null,
            color: Theme.of(context).accentColor,
            child: Center(
              child: Text(LocaleKeys.generate_otp.tr()),
            ),
          ),
        );
      },
    );
  }
}

My Login bloc:

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:cheruvu/login/model/phonenumber.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:formz/formz.dart';

part 'login_event.dart';
part 'login_state.dart';

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  @override
  LoginState get initialState => LoginState();

  @override
  Stream<LoginState> mapEventToState(
    LoginEvent event,
  ) async* {
    if (event is PhoneNumberChanged) {
      final phoneNumber = PhoneNumber.dirty(event.phoneNumber);
      yield state.copyWith(
          phoneNumber: phoneNumber, status: Formz.validate([phoneNumber]));
    } else if (event is PhoneNumberSubmitted) {
      if (state.status.isValidated) {
        yield state.copyWith(status: FormzStatus.submissionSuccess);
      }
    }
  }
}
example question

Most helpful comment

Hi @jaydangar 馃憢
Thanks for opening an issue!

I think you're missing the import for formz. Try adding:

import 'package:formz/formz.dart`;

to your imports and also you need to change the line to state.status.isSubmissionSuccess.

Hope that helps 馃憤

All 2 comments

Hi @jaydangar 馃憢
Thanks for opening an issue!

I think you're missing the import for formz. Try adding:

import 'package:formz/formz.dart`;

to your imports and also you need to change the line to state.status.isSubmissionSuccess.

Hope that helps 馃憤

@felangel Thanks, It's I guess static method or enum, This happens alot with flutter and other packages. Appreciate your help.

Was this page helpful?
0 / 5 - 0 ratings